[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-10-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks!




Comment at: 
clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp:35-41
+   {
+   R"cpp(
+#include "Te^stTU.h"
+)cpp",
+   true,
+   {}},
+   {"void foo(^) {}", false, {}}};

kadircet wrote:
> nit: you can use EXPECT_AVAILABLE and EXPECT_UNAVAILABLE directly for these 
> two cases
I think it's the other way around. I can use the macros directly when I don't 
need to pass specific requested action kinds, which is the last (third) case. 
So it's probably not worth it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-10-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 557771.
VitaNuo marked 10 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/test/request-reply.test
  clang-tools-extra/clangd/test/tweaks-format.test
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h

Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
@@ -18,6 +18,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -88,13 +89,17 @@
   //  - if the tweak produces a message, returns "message:\n"
   //  - if prepare() returns false, returns "unavailable"
   //  - if apply() returns an error, returns "fail: "
-  std::string apply(llvm::StringRef MarkedCode,
-llvm::StringMap *EditedFiles = nullptr) const;
+  std::string
+  apply(llvm::StringRef MarkedCode,
+llvm::StringMap *EditedFiles = nullptr,
+const std::vector  = {}) const;
 
   // Helpers for EXPECT_AVAILABLE/EXPECT_UNAVAILABLE macros.
   using WrappedAST = std::pair;
   WrappedAST build(llvm::StringRef) const;
-  bool isAvailable(WrappedAST &, llvm::Annotations::Range) const;
+  bool
+  isAvailable(WrappedAST &, llvm::Annotations::Range,
+  const std::vector  = {}) const;
   // Return code re-decorated with a single point/range.
   static std::string decorate(llvm::StringRef, unsigned);
   static std::string decorate(llvm::StringRef, llvm::Annotations::Range);
@@ -116,9 +121,10 @@
 auto AST = build(A.code());\
 assert(!A.points().empty() || !A.ranges().empty());\
 for (const auto  : A.points())   \
-  EXPECT_EQ(Available, isAvailable(AST, {P, P})) << decorate(A.code(), P); \
+  EXPECT_EQ(Available, isAvailable(AST, {P, P}, {}))   \
+  << decorate(A.code(), P);\
 for (const auto  : A.ranges())   \
-  EXPECT_EQ(Available, isAvailable(AST, R)) << decorate(A.code(), R);  \
+  EXPECT_EQ(Available, isAvailable(AST, R, {})) << decorate(A.code(), R);  \
   } while (0)
 #define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true)
 #define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false)
Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
@@ -8,6 +8,7 @@
 
 #include "TweakTesting.h"
 
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "refactor/Tweak.h"
@@ -16,6 +17,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -63,12 +65,14 @@
 // Returns std::nullopt if and only if prepare() failed.
 std::optional>
 applyTweak(ParsedAST , llvm::Annotations::Range Range, StringRef TweakID,
-   const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
+   const SymbolIndex *Index, llvm::vfs::FileSystem *FS,
+   const std::vector ) {
   std::optional> Result;
   SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
 Range.End, [&](SelectionTree ST) {
   Tweak::Selection S(Index, AST, Range.Begin,
- Range.End, std::move(ST), FS);
+ Range.End, std::move(ST), FS,
+ 

[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-12 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Respond to comments.




Comment at: clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp:56
+return false;
+  Range PreambleRange;
+  PreambleRange.start =

kadircet wrote:
> VitaNuo wrote:
> > kadircet wrote:
> > > kadircet wrote:
> > > > relying on `MainFileIncludes` for preamble range is not correct in 
> > > > general. we can have includes way down inside the main file that'll be 
> > > > part of this vector, but not preamble (also it's not sorted explicitly).
> > > > 
> > > > we should instead use `ComputePreambleBounds` (nit: we can also store 
> > > > it in ParsedAST, instead of relexing the file one more time with each 
> > > > CodeAction request)
> > > Having a comment for reason would also be helpful, something like `To 
> > > accommodate clients without knowledge of source actions, we trigger 
> > > without checking code action kinds when inside the preamble region`.
> > > nit: we can also store it in ParsedAST
> > 
> > Seems like the data is already there, I just need to expose it.
> > > nit: we can also store it in ParsedAST
> > Seems like the data is already there, I just need to expose it.
> 
> not really, we calculate bounds when building a `ParsedAST` but we don't 
> store it anywhere.
I was referring to 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Frontend/PrecompiledPreamble.cpp#L580.
 We store the `PreambleData` and that seems to store the `PrecompiledPreamble` 
which seems to know its bounds


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-12 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

I think I also managed to update the wrong patch. Not sure how an alternative 
patch got created in the first place :( Sorry for the mess, should be fine now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-12 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556532.
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/test/request-reply.test
  clang-tools-extra/clangd/test/tweaks-format.test
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h

Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
@@ -18,6 +18,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -88,13 +89,16 @@
   //  - if the tweak produces a message, returns "message:\n"
   //  - if prepare() returns false, returns "unavailable"
   //  - if apply() returns an error, returns "fail: "
-  std::string apply(llvm::StringRef MarkedCode,
-llvm::StringMap *EditedFiles = nullptr) const;
+  std::string
+  apply(llvm::StringRef MarkedCode,
+llvm::StringMap *EditedFiles = nullptr,
+const std::vector  = {}) const;
 
   // Helpers for EXPECT_AVAILABLE/EXPECT_UNAVAILABLE macros.
   using WrappedAST = std::pair;
   WrappedAST build(llvm::StringRef) const;
-  bool isAvailable(WrappedAST &, llvm::Annotations::Range) const;
+  bool isAvailable(WrappedAST &, llvm::Annotations::Range,
+   const std::vector & = {}) const;
   // Return code re-decorated with a single point/range.
   static std::string decorate(llvm::StringRef, unsigned);
   static std::string decorate(llvm::StringRef, llvm::Annotations::Range);
@@ -116,9 +120,10 @@
 auto AST = build(A.code());\
 assert(!A.points().empty() || !A.ranges().empty());\
 for (const auto  : A.points())   \
-  EXPECT_EQ(Available, isAvailable(AST, {P, P})) << decorate(A.code(), P); \
+  EXPECT_EQ(Available, isAvailable(AST, {P, P}, {}))   \
+  << decorate(A.code(), P);\
 for (const auto  : A.ranges())   \
-  EXPECT_EQ(Available, isAvailable(AST, R)) << decorate(A.code(), R);  \
+  EXPECT_EQ(Available, isAvailable(AST, R, {})) << decorate(A.code(), R);  \
   } while (0)
 #define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true)
 #define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false)
Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
@@ -8,6 +8,7 @@
 
 #include "TweakTesting.h"
 
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "refactor/Tweak.h"
@@ -16,6 +17,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -63,12 +65,14 @@
 // Returns std::nullopt if and only if prepare() failed.
 std::optional>
 applyTweak(ParsedAST , llvm::Annotations::Range Range, StringRef TweakID,
-   const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
+   const SymbolIndex *Index, llvm::vfs::FileSystem *FS,
+   const std::vector ) {
   std::optional> Result;
   SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
 Range.End, [&](SelectionTree ST) {
   Tweak::Selection S(Index, AST, Range.Begin,
- Range.End, std::move(ST), FS);
+ Range.End, std::move(ST), FS,
+ RequestedActionKinds);
   

[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-12 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked 2 inline comments as done.
VitaNuo added a comment.

I'm sorry I think the upload failed because I tried to upload without a message 
or something, and I didn't notice that it failed :(


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D159498: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-12 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556531.
VitaNuo added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159498/new/

https://reviews.llvm.org/D159498

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/test/request-reply.test
  clang-tools-extra/clangd/test/tweaks-format.test
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h

Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
@@ -18,6 +18,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -88,13 +89,16 @@
   //  - if the tweak produces a message, returns "message:\n"
   //  - if prepare() returns false, returns "unavailable"
   //  - if apply() returns an error, returns "fail: "
-  std::string apply(llvm::StringRef MarkedCode,
-llvm::StringMap *EditedFiles = nullptr) const;
+  std::string
+  apply(llvm::StringRef MarkedCode,
+llvm::StringMap *EditedFiles = nullptr,
+const std::vector  = {}) const;
 
   // Helpers for EXPECT_AVAILABLE/EXPECT_UNAVAILABLE macros.
   using WrappedAST = std::pair;
   WrappedAST build(llvm::StringRef) const;
-  bool isAvailable(WrappedAST &, llvm::Annotations::Range) const;
+  bool isAvailable(WrappedAST &, llvm::Annotations::Range,
+   const std::vector & = {}) const;
   // Return code re-decorated with a single point/range.
   static std::string decorate(llvm::StringRef, unsigned);
   static std::string decorate(llvm::StringRef, llvm::Annotations::Range);
@@ -116,9 +120,10 @@
 auto AST = build(A.code());\
 assert(!A.points().empty() || !A.ranges().empty());\
 for (const auto  : A.points())   \
-  EXPECT_EQ(Available, isAvailable(AST, {P, P})) << decorate(A.code(), P); \
+  EXPECT_EQ(Available, isAvailable(AST, {P, P}, {}))   \
+  << decorate(A.code(), P);\
 for (const auto  : A.ranges())   \
-  EXPECT_EQ(Available, isAvailable(AST, R)) << decorate(A.code(), R);  \
+  EXPECT_EQ(Available, isAvailable(AST, R, {})) << decorate(A.code(), R);  \
   } while (0)
 #define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true)
 #define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false)
Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
@@ -8,6 +8,7 @@
 
 #include "TweakTesting.h"
 
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "refactor/Tweak.h"
@@ -16,6 +17,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -63,12 +65,14 @@
 // Returns std::nullopt if and only if prepare() failed.
 std::optional>
 applyTweak(ParsedAST , llvm::Annotations::Range Range, StringRef TweakID,
-   const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
+   const SymbolIndex *Index, llvm::vfs::FileSystem *FS,
+   const std::vector ) {
   std::optional> Result;
   SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
 Range.End, [&](SelectionTree ST) {
   Tweak::Selection S(Index, AST, Range.Begin,
- Range.End, std::move(ST), FS);
+ Range.End, std::move(ST), FS,
+ RequestedActionKinds);
   if (auto T = prepareTweak(TweakID, S, nullptr)) 

[PATCH] D159498: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159498

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/test/request-reply.test
  clang-tools-extra/clangd/test/tweaks-format.test
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h

Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.h
@@ -18,6 +18,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -88,13 +89,16 @@
   //  - if the tweak produces a message, returns "message:\n"
   //  - if prepare() returns false, returns "unavailable"
   //  - if apply() returns an error, returns "fail: "
-  std::string apply(llvm::StringRef MarkedCode,
-llvm::StringMap *EditedFiles = nullptr) const;
+  std::string
+  apply(llvm::StringRef MarkedCode,
+llvm::StringMap *EditedFiles = nullptr,
+const std::vector  = {}) const;
 
   // Helpers for EXPECT_AVAILABLE/EXPECT_UNAVAILABLE macros.
   using WrappedAST = std::pair;
   WrappedAST build(llvm::StringRef) const;
-  bool isAvailable(WrappedAST &, llvm::Annotations::Range) const;
+  bool isAvailable(WrappedAST &, llvm::Annotations::Range,
+   const std::vector & = {}) const;
   // Return code re-decorated with a single point/range.
   static std::string decorate(llvm::StringRef, unsigned);
   static std::string decorate(llvm::StringRef, llvm::Annotations::Range);
@@ -116,9 +120,10 @@
 auto AST = build(A.code());\
 assert(!A.points().empty() || !A.ranges().empty());\
 for (const auto  : A.points())   \
-  EXPECT_EQ(Available, isAvailable(AST, {P, P})) << decorate(A.code(), P); \
+  EXPECT_EQ(Available, isAvailable(AST, {P, P}, {}))   \
+  << decorate(A.code(), P);\
 for (const auto  : A.ranges())   \
-  EXPECT_EQ(Available, isAvailable(AST, R)) << decorate(A.code(), R);  \
+  EXPECT_EQ(Available, isAvailable(AST, R, {})) << decorate(A.code(), R);  \
   } while (0)
 #define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true)
 #define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false)
Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
@@ -8,6 +8,7 @@
 
 #include "TweakTesting.h"
 
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "refactor/Tweak.h"
@@ -16,6 +17,7 @@
 #include "gtest/gtest.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -63,12 +65,14 @@
 // Returns std::nullopt if and only if prepare() failed.
 std::optional>
 applyTweak(ParsedAST , llvm::Annotations::Range Range, StringRef TweakID,
-   const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
+   const SymbolIndex *Index, llvm::vfs::FileSystem *FS,
+   const std::vector ) {
   std::optional> Result;
   SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
 Range.End, [&](SelectionTree ST) {
   Tweak::Selection S(Index, AST, Range.Begin,
- Range.End, std::move(ST), FS);
+ Range.End, std::move(ST), FS,
+ 

[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-09-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked 10 inline comments as done.
VitaNuo added a comment.

Thank you for the comments!




Comment at: clang-tools-extra/clangd/ClangdServer.cpp:752
   auto Action = [File = File.str(), Sel, TweakID = TweakID.str(),
- CB = std::move(CB),
+ CB = std::move(CB), ActionKinds,
  this](Expected InpAST) mutable {

kadircet wrote:
> we're capturing `ActionKinds` as a reference, but Action is going to execute 
> async. you can instead change the input to be `llvm::ArrayRef 
> ActionKinds` and capture it by value with `ActionKinds = ActionKinds.vec()`
> 
> nit: even better if you just changed the signature here to look like 
> `applyTweak(std::string TweakID, CodeActionInputs Inputs, 
> Callback CB)`, as we want to consume all of these by value 
> anyways. Then you can just move `TweakID` and `Inputs` into the `Action`.
Ok, I can change the signature, but it seems like I still need to move 
individual members of `CodeActionInputs` into the callback separately. `File` 
cannot be moved, since it's also needed after the callback as an argument to 
`runWithAST`.



Comment at: clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp:56
+return false;
+  Range PreambleRange;
+  PreambleRange.start =

kadircet wrote:
> kadircet wrote:
> > relying on `MainFileIncludes` for preamble range is not correct in general. 
> > we can have includes way down inside the main file that'll be part of this 
> > vector, but not preamble (also it's not sorted explicitly).
> > 
> > we should instead use `ComputePreambleBounds` (nit: we can also store it in 
> > ParsedAST, instead of relexing the file one more time with each CodeAction 
> > request)
> Having a comment for reason would also be helpful, something like `To 
> accommodate clients without knowledge of source actions, we trigger without 
> checking code action kinds when inside the preamble region`.
> nit: we can also store it in ParsedAST

Seems like the data is already there, I just need to expose it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG64366d4935d3: [clangd] Rollforward include-cleaner library 
usage in symbol collector. (authored by VitaNuo).

Changed prior to commit:
  https://reviews.llvm.org/D156659?vs=556135=556396#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/IndexActionTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -14,6 +14,7 @@
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexingOptions.h"
@@ -1554,6 +1555,8 @@
 // Move overloads have special handling.
 template  T&& move(_T&& __value);
 template  _O move(_I, _I, _O);
+template  _O move(
+  _T&&, _O, _O, _I);
   }
   )cpp",
   /*Main=*/"");
@@ -1565,7 +1568,8 @@
 includeHeader("")),
   // Parameter names are demangled.
   AllOf(labeled("move(T &)"), includeHeader("")),
-  AllOf(labeled("move(I, I, O)"), includeHeader("";
+  AllOf(labeled("move(I, I, O)"), includeHeader("")),
+  AllOf(labeled("move(T &&, O, O, I)"), includeHeader("";
 }
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
@@ -1592,7 +1596,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
@@ -1978,6 +1982,24 @@
   qName("A"), hasKind(clang::index::SymbolKind::Concept;
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once 
+struct Foo;
+#include "full.h"
+)cpp";
+  auto FullFile = testPath("full.h");
+  InMemoryFileSystem->addFile(FullFile, 0,
+  llvm::MemoryBuffer::getMemBuffer(R"cpp(
+#pragma once 
+struct Foo {};)cpp"));
+  runSymbolCollector(Header, /*Main=*/"",
+ /*ExtraArgs=*/{"-I", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+   qName("Foo"),
+   includeHeader(URI::create(FullFile).toString()
+  << *Symbols.begin();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/unittests/IndexActionTests.cpp
===
--- clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -8,11 +8,15 @@
 
 #include "Headers.h"
 #include "TestFS.h"
+#include "URI.h"
 #include "index/IndexAction.h"
 #include "index/Serialization.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Tooling.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -40,6 +44,11 @@
   return toUri(Path) == URI;
 }
 
+MATCHER_P(includeHeader, P, "") {
+  return (arg.IncludeHeaders.size() == 1) &&
+ (arg.IncludeHeaders.begin()->IncludeHeader == P);
+}
+
 ::testing::Matcher
 includesAre(const std::vector ) {
   return ::testing::Field(::DirectIncludes,
@@ -312,6 +321,26 @@
   EXPECT_THAT(*IndexFile.Symbols, testing::Contains(hasName("Bar")));
   EXPECT_THAT(*IndexFile.Symbols, Not(testing::Contains(hasName("Baz";
 }
+
+TEST_F(IndexActionTest, SymbolFromCC) {
+  std::string MainFilePath = testPath("main.cpp");
+  addFile(MainFilePath, R"cpp(
+ #include "main.h"
+ void foo() {}
+ )cpp");
+  addFile(testPath("main.h"), R"cpp(
+ #pragma once
+ void foo();
+ )cpp");
+  Opts.FileFilter = [](const SourceManager , FileID F) {
+return !SM.getFileEntryRefForID(F)->getName().endswith("main.h");
+  };
+  IndexFileIn IndexFile = runIndexingAction(MainFilePath, {"-std=c++14"});
+  EXPECT_THAT(*IndexFile.Symbols,
+  UnorderedElementsAre(AllOf(
+  hasName("foo"),
+  includeHeader(URI::create(testPath("main.h")).toString();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: 

[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556135.
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/IndexActionTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp

Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,7 +125,7 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -14,6 +14,7 @@
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexingOptions.h"
@@ -1554,6 +1555,8 @@
 // Move overloads have special handling.
 template  T&& move(_T&& __value);
 template  _O move(_I, _I, _O);
+template  _O move(
+  _T&&, _O, _O, _I);
   }
   )cpp",
   /*Main=*/"");
@@ -1565,7 +1568,8 @@
 includeHeader("")),
   // Parameter names are demangled.
   AllOf(labeled("move(T &)"), includeHeader("")),
-  AllOf(labeled("move(I, I, O)"), includeHeader("";
+  AllOf(labeled("move(I, I, O)"), includeHeader("")),
+  AllOf(labeled("move(T &&, O, O, I)"), includeHeader("";
 }
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
@@ -1592,7 +1596,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
@@ -1978,6 +1982,24 @@
   qName("A"), hasKind(clang::index::SymbolKind::Concept;
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once 
+struct Foo;
+#include "full.h"
+)cpp";
+  auto FullFile = testPath("full.h");
+  InMemoryFileSystem->addFile(FullFile, 0,
+  llvm::MemoryBuffer::getMemBuffer(R"cpp(
+#pragma once 
+struct Foo {};)cpp"));
+  runSymbolCollector(Header, /*Main=*/"",
+ /*ExtraArgs=*/{"-I", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+   qName("Foo"),
+   includeHeader(URI::create(FullFile).toString()
+  << *Symbols.begin();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/unittests/IndexActionTests.cpp
===
--- clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -8,11 +8,15 @@
 
 #include "Headers.h"
 #include "TestFS.h"
+#include "URI.h"
 #include "index/IndexAction.h"
 #include "index/Serialization.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Tooling.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -40,6 +44,11 @@
   return toUri(Path) == URI;
 }
 
+MATCHER_P(includeHeader, P, "") {
+  return (arg.IncludeHeaders.size() == 1) &&
+ (arg.IncludeHeaders.begin()->IncludeHeader == P);
+}
+
 ::testing::Matcher
 includesAre(const std::vector ) {
   return ::testing::Field(::DirectIncludes,
@@ -312,6 +321,26 @@
   EXPECT_THAT(*IndexFile.Symbols, testing::Contains(hasName("Bar")));
   EXPECT_THAT(*IndexFile.Symbols, Not(testing::Contains(hasName("Baz";
 }
+
+TEST_F(IndexActionTest, SymbolFromCC) {
+  std::string MainFilePath = testPath("main.cpp");
+  addFile(MainFilePath, R"cpp(
+ #include "main.h"
+ void foo() {}
+ )cpp");
+  addFile(testPath("main.h"), R"cpp(
+ #pragma once
+ void foo();
+ )cpp");
+  

[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked 2 inline comments as done.
VitaNuo added inline comments.



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:919
+  if (IncludeHeader.empty())
+HeaderFileURIs->getIncludeHeader(FID);
+

kadircet wrote:
> i guess LHS of the assignment got dropped by mistake?
Sorry, juggling unconnected stuff makes me inattentive.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556130.
VitaNuo marked 2 inline comments as done.
VitaNuo added a comment.

Address the comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/IndexActionTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp

Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,7 +125,7 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -14,6 +14,7 @@
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexingOptions.h"
@@ -1554,6 +1555,8 @@
 // Move overloads have special handling.
 template  T&& move(_T&& __value);
 template  _O move(_I, _I, _O);
+template  _O move(
+  _T&&, _O, _O, _I);
   }
   )cpp",
   /*Main=*/"");
@@ -1565,7 +1568,8 @@
 includeHeader("")),
   // Parameter names are demangled.
   AllOf(labeled("move(T &)"), includeHeader("")),
-  AllOf(labeled("move(I, I, O)"), includeHeader("";
+  AllOf(labeled("move(I, I, O)"), includeHeader("")),
+  AllOf(labeled("move(T &&, O, O, I)"), includeHeader("";
 }
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
@@ -1592,7 +1596,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
@@ -1978,6 +1982,24 @@
   qName("A"), hasKind(clang::index::SymbolKind::Concept;
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once 
+struct Foo;
+#include "full.h"
+)cpp";
+  auto FullFile = testPath("full.h");
+  InMemoryFileSystem->addFile(FullFile, 0,
+  llvm::MemoryBuffer::getMemBuffer(R"cpp(
+#pragma once 
+struct Foo {};)cpp"));
+  runSymbolCollector(Header, /*Main=*/"",
+ /*ExtraArgs=*/{"-I", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+   qName("Foo"),
+   includeHeader(URI::create(FullFile).toString()
+  << *Symbols.begin();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/unittests/IndexActionTests.cpp
===
--- clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -8,11 +8,15 @@
 
 #include "Headers.h"
 #include "TestFS.h"
+#include "URI.h"
 #include "index/IndexAction.h"
 #include "index/Serialization.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Tooling.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -40,6 +44,11 @@
   return toUri(Path) == URI;
 }
 
+MATCHER_P(includeHeader, P, "") {
+  return (arg.IncludeHeaders.size() == 1) &&
+ (arg.IncludeHeaders.begin()->IncludeHeader == P);
+}
+
 ::testing::Matcher
 includesAre(const std::vector ) {
   return ::testing::Field(::DirectIncludes,
@@ -312,6 +321,26 @@
   EXPECT_THAT(*IndexFile.Symbols, testing::Contains(hasName("Bar")));
   EXPECT_THAT(*IndexFile.Symbols, Not(testing::Contains(hasName("Baz";
 }
+
+TEST_F(IndexActionTest, SymbolFromCC) {
+  std::string MainFilePath = testPath("main.cpp");
+  addFile(MainFilePath, R"cpp(
+ #include "main.h"
+ void foo() {}
+ )cpp");
+  addFile(testPath("main.h"), R"cpp(
+ #pragma once
+ void foo();
+ )cpp");
+  

[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added inline comments.



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:912
+if (Directives & Symbol::Import) {
+  if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
+  !IncludeHeader.empty()) {

kadircet wrote:
> we should keep the `getStdHeaders` logic for objc
`getStdHeaders` returns empty string for Obj-C, are you sure you meant it?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG43c20367f417: [include-cleaner][clangd][clang-tidy] Ignore 
resource dir during include… (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -271,6 +271,31 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("resources");
+  Inputs.ExtraArgs.push_back("-internal-isystem");
+  Inputs.ExtraArgs.push_back("resources/include");
+  Inputs.Code = R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/include/amintrin.h"] = guard("");
+  Inputs.ExtraFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.preprocessor());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -13,6 +13,7 @@
 #include "clang-include-cleaner/Types.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
+#include "clang/Basic/DirectoryEntry.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
@@ -86,12 +87,17 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  const DirectoryEntry *ResourceDir =
+  PP.getHeaderSearchInfo().getModuleMap().getBuiltinDir();
   walkUsed(ASTRoots, MacroRefs, PI, PP,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getDir() == ResourceDir)) {
  Satisfied = true;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -107,7 +113,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getFileEntry().getDir() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/include/amintrin.h"] = guard("");
+  TU.AdditionalFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TU.AdditionalFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test

[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556127.
VitaNuo marked 3 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -271,6 +271,31 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("resources");
+  Inputs.ExtraArgs.push_back("-internal-isystem");
+  Inputs.ExtraArgs.push_back("resources/include");
+  Inputs.Code = R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/include/amintrin.h"] = guard("");
+  Inputs.ExtraFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.preprocessor());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -13,6 +13,7 @@
 #include "clang-include-cleaner/Types.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
+#include "clang/Basic/DirectoryEntry.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
@@ -86,12 +87,17 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  const DirectoryEntry *ResourceDir =
+  PP.getHeaderSearchInfo().getModuleMap().getBuiltinDir();
   walkUsed(ASTRoots, MacroRefs, PI, PP,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getDir() == ResourceDir)) {
  Satisfied = true;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -107,7 +113,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getFileEntry().getDir() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/include/amintrin.h"] = guard("");
+  TU.AdditionalFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TU.AdditionalFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
===
--- clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
+++ 

[PATCH] D159462: [clangd][clang-tidy] Add missing symbols to the symbol map.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG625891288087: [clangd][clang-tidy] Add missing symbols to 
the symbol map. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159462/new/

https://reviews.llvm.org/D159462

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -364,6 +364,8 @@
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
 SYMBOL(any_cast, std::, )
+SYMBOL(div, std::, )
+SYMBOL(abort, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -364,6 +364,8 @@
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
 SYMBOL(any_cast, std::, )
+SYMBOL(div, std::, )
+SYMBOL(abort, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D159463: [include-cleaner] Map the 4-argument move overload to the algorithm header.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd71adebb9fb8: [include-cleaner] Map the 4-argument move 
overload to the algorithm header. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159463/new/

https://reviews.llvm.org/D159463

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -546,6 +546,16 @@
   "move",
   "",
   },
+  {
+  R"cpp(
+namespace std {
+ template
+ ForwardIt2 move(ExecutionPolicy&& policy,
+ ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first);
+})cpp",
+  "move",
+  "",
+  },
   {
   R"cpp(
 namespace std {
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,8 +125,10 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
+  // move(ExecutionPolicy&& policy, ForwardIt1 first,
+  // ForwardIt1 last, ForwardIt2 d_first);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
 if (FD->getNumParams() == 1)


Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -546,6 +546,16 @@
   "move",
   "",
   },
+  {
+  R"cpp(
+namespace std {
+ template
+ ForwardIt2 move(ExecutionPolicy&& policy,
+ ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first);
+})cpp",
+  "move",
+  "",
+  },
   {
   R"cpp(
 namespace std {
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,8 +125,10 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
+  // move(ExecutionPolicy&& policy, ForwardIt1 first,
+  // ForwardIt1 last, ForwardIt2 d_first);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
 if (FD->getNumParams() == 1)
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 556004.
VitaNuo marked 5 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156659/new/

https://reviews.llvm.org/D156659

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/IndexActionTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp

Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,7 +125,7 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -14,6 +14,7 @@
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexingOptions.h"
@@ -1554,6 +1555,8 @@
 // Move overloads have special handling.
 template  T&& move(_T&& __value);
 template  _O move(_I, _I, _O);
+template  _O move(
+  _T&&, _O, _O, _I);
   }
   )cpp",
   /*Main=*/"");
@@ -1565,7 +1568,8 @@
 includeHeader("")),
   // Parameter names are demangled.
   AllOf(labeled("move(T &)"), includeHeader("")),
-  AllOf(labeled("move(I, I, O)"), includeHeader("";
+  AllOf(labeled("move(I, I, O)"), includeHeader("")),
+  AllOf(labeled("move(T &&, O, O, I)"), includeHeader("";
 }
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
@@ -1592,7 +1596,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
@@ -1978,6 +1982,24 @@
   qName("A"), hasKind(clang::index::SymbolKind::Concept;
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once 
+struct Foo;
+#include "full.h"
+)cpp";
+  auto FullFile = testPath("full.h");
+  InMemoryFileSystem->addFile(FullFile, 0,
+  llvm::MemoryBuffer::getMemBuffer(R"cpp(
+#pragma once 
+struct Foo {};)cpp"));
+  runSymbolCollector(Header, /*Main=*/"",
+ /*ExtraArgs=*/{"-I", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+   qName("Foo"),
+   includeHeader(URI::create(FullFile).toString()
+  << *Symbols.begin();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/unittests/IndexActionTests.cpp
===
--- clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -8,11 +8,15 @@
 
 #include "Headers.h"
 #include "TestFS.h"
+#include "URI.h"
 #include "index/IndexAction.h"
 #include "index/Serialization.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Tooling.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include 
 
 namespace clang {
 namespace clangd {
@@ -40,6 +44,11 @@
   return toUri(Path) == URI;
 }
 
+MATCHER_P(includeHeader, P, "") {
+  return (arg.IncludeHeaders.size() == 1) &&
+ (arg.IncludeHeaders.begin()->IncludeHeader == P);
+}
+
 ::testing::Matcher
 includesAre(const std::vector ) {
   return ::testing::Field(::DirectIncludes,
@@ -312,6 +321,26 @@
   EXPECT_THAT(*IndexFile.Symbols, testing::Contains(hasName("Bar")));
   EXPECT_THAT(*IndexFile.Symbols, Not(testing::Contains(hasName("Baz";
 }
+
+TEST_F(IndexActionTest, SymbolFromCC) {
+  std::string MainFilePath = testPath("main.cpp");
+  addFile(MainFilePath, R"cpp(
+ #include "main.h"
+ void foo() {}
+ )cpp");
+  addFile(testPath("main.h"), R"cpp(
+ #pragma once
+ void foo();
+ )cpp");

[PATCH] D159463: [include-cleaner] Map the 4-argument move overload to the algorithm header.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added a subscriber: kadircet.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159463

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -546,6 +546,16 @@
   "move",
   "",
   },
+  {
+  R"cpp(
+namespace std {
+ template
+ ForwardIt2 move(ExecutionPolicy&& policy,
+ ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first);
+})cpp",
+  "move",
+  "",
+  },
   {
   R"cpp(
 namespace std {
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,8 +125,10 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
+  // move(ExecutionPolicy&& policy, ForwardIt1 first,
+  // ForwardIt1 last, ForwardIt2 d_first);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
 if (FD->getNumParams() == 1)


Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -546,6 +546,16 @@
   "move",
   "",
   },
+  {
+  R"cpp(
+namespace std {
+ template
+ ForwardIt2 move(ExecutionPolicy&& policy,
+ ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first);
+})cpp",
+  "move",
+  "",
+  },
   {
   R"cpp(
 namespace std {
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -125,8 +125,10 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
+  // move(ExecutionPolicy&& policy, ForwardIt1 first,
+  // ForwardIt1 last, ForwardIt2 d_first);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
 if (FD->getNumParams() == 1)
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D159462: [clangd][clang-tidy] Add missing symbols to the symbol map.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman, xazax.hun.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159462

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -364,6 +364,8 @@
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
 SYMBOL(any_cast, std::, )
+SYMBOL(div, std::, )
+SYMBOL(abort, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -364,6 +364,8 @@
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
 SYMBOL(any_cast, std::, )
+SYMBOL(div, std::, )
+SYMBOL(abort, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 555995.
VitaNuo added a comment.

Rebase to current main.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -271,6 +271,31 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("resources");
+  Inputs.ExtraArgs.push_back("-internal-isystem");
+  Inputs.ExtraArgs.push_back("resources/include");
+  Inputs.Code = R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/include/amintrin.h"] = "";
+  Inputs.ExtraFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.preprocessor());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -13,6 +13,7 @@
 #include "clang-include-cleaner/Types.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
+#include "clang/Basic/DirectoryEntry.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
@@ -86,12 +87,18 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  const DirectoryEntry *ResourceDir =
+  PP.getHeaderSearchInfo().getModuleMap().getBuiltinDir();
   walkUsed(ASTRoots, MacroRefs, PI, PP,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getDir() == ResourceDir)) {
  Satisfied = true;
+ continue;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -107,7 +114,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getFileEntry().getDir() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/include/amintrin.h"] = "";
+  TU.AdditionalFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TU.AdditionalFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
===
--- clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
+++ clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
@@ -5,10 

[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Thanks for the help in resolving the resource dir issues!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-09-06 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 555985.
VitaNuo added a comment.

Fix analysis test to properly recognize the resource directory.
Use HeaderSearch->getModuleMap().BuiltinDir to find out the resource directory.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -273,6 +273,32 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("resources");
+  Inputs.ExtraArgs.push_back("-internal-isystem");
+  Inputs.ExtraArgs.push_back("resources/include");
+  Inputs.Code = R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/include/amintrin.h"] = "";
+  Inputs.ExtraFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.sourceManager(),
+ AST.preprocessor().getHeaderSearchInfo());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -13,6 +13,7 @@
 #include "clang-include-cleaner/Types.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclBase.h"
+#include "clang/Basic/DirectoryEntry.h"
 #include "clang/Basic/FileEntry.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
@@ -68,12 +69,17 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  const DirectoryEntry *ResourceDir = HS.getModuleMap().getBuiltinDir();
   walkUsed(ASTRoots, MacroRefs, PI, SM,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getDir() == ResourceDir)) {
  Satisfied = true;
+ continue;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -88,7 +94,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getFileEntry().getDir() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/include/amintrin.h"] = "";
+  TU.AdditionalFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TU.AdditionalFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test

[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-08-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the comments!




Comment at: clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp:122
   llvm::DenseSet SeenSymbols;
+  std::string ResourceDir = HS->getHeaderSearchOpts().ResourceDir;
   // FIXME: Find a way to have less code duplication between include-cleaner

kadircet wrote:
> let's use `HS->getModuleMap().getBuiltinDir()` then we can get away with just 
> comparing that pointer to `H.physical()->getLastRef().getDir()` (same applies 
> to all the other places as well)
This only works in `clangd` code for me. I get `nullptr` for 
`HS->getModuleMap().getBuiltinDir()` in other places (Clang Tidy check and the 
library).



Comment at: clang-tools-extra/clangd/IncludeCleaner.cpp:309
   continue;
+auto Dir = llvm::StringRef{MFI.Resolved}.rsplit('/').first;
+if (Dir == AST.getPreprocessor()

kadircet wrote:
> let's move this into `mayConsiderUnused`, we also convert this include into a 
> FileEntry in there, so we can directly compare the directory agian.
Ok moved to `mayConsiderUnused` and using the file entry now, but see above 
regarding comparing the directories.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-08-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 549443.
VitaNuo marked 4 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157610/new/

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -273,6 +273,30 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("./resources");
+  Inputs.Code = R"cpp(
+#include "resources/amintrin.h"
+#include "resources/imintrin.h"
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/amintrin.h"] = "";
+  Inputs.ExtraFiles["resources/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/imintrin.h"] = guard(R"cpp(
+#include "emintrin.h"
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.sourceManager(),
+ AST.preprocessor().getHeaderSearchInfo());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -17,6 +17,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
 #include "clang/Lex/HeaderSearch.h"
+#include "clang/Lex/HeaderSearchOptions.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
@@ -68,12 +69,18 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  std::string ResourceDir = HS.getHeaderSearchOpts().ResourceDir;
   walkUsed(ASTRoots, MacroRefs, PI, SM,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getLastRef().getDir().getName() ==
+ResourceDir)) {
  Satisfied = true;
+ continue;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -88,7 +95,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getDir().getName() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include 
+#include 
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/include/amintrin.h"] = "";
+  TU.AdditionalFiles["resources/include/imintrin.h"] = guard(R"cpp(
+#include 
+  )cpp");
+  TU.AdditionalFiles["resources/include/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
===
--- clang-tools-extra/clangd/test/include-cleaner-batch-fix.test

[PATCH] D157610: [include-cleaner][clangd][clang-tidy] Ignore resource dir during include-cleaner analysis.

2023-08-10 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: PiotrZSL, kadircet, carlosgalvezp, arphaman, 
xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157610

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
@@ -273,6 +273,30 @@
   EXPECT_THAT(Results.Unused, testing::IsEmpty());
 }
 
+TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
+  Inputs.ExtraArgs.push_back("-resource-dir");
+  Inputs.ExtraArgs.push_back("./resources");
+  Inputs.Code = R"cpp(
+#include "resources/amintrin.h"
+#include "resources/imintrin.h"
+void baz() {
+  bar();
+}
+  )cpp";
+  Inputs.ExtraFiles["resources/amintrin.h"] = "";
+  Inputs.ExtraFiles["resources/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  Inputs.ExtraFiles["resources/imintrin.h"] = guard(R"cpp(
+#include "emintrin.h"
+  )cpp");
+  TestAST AST(Inputs);
+  auto Results = analyze({}, {}, PP.Includes, , AST.sourceManager(),
+ AST.preprocessor().getHeaderSearchInfo());
+  EXPECT_THAT(Results.Unused, testing::IsEmpty());
+  EXPECT_THAT(Results.Missing, testing::IsEmpty());
+}
+
 TEST(FixIncludes, Basic) {
   llvm::StringRef Code = R"cpp(#include "d.h"
 #include "a.h"
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -17,6 +17,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Format/Format.h"
 #include "clang/Lex/HeaderSearch.h"
+#include "clang/Lex/HeaderSearchOptions.h"
 #include "clang/Tooling/Core/Replacement.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
@@ -68,12 +69,18 @@
   llvm::StringSet<> Missing;
   if (!HeaderFilter)
 HeaderFilter = [](llvm::StringRef) { return false; };
+  std::string ResourceDir = HS.getHeaderSearchOpts().ResourceDir;
   walkUsed(ASTRoots, MacroRefs, PI, SM,
[&](const SymbolReference , llvm::ArrayRef Providers) {
  bool Satisfied = false;
  for (const Header  : Providers) {
-   if (H.kind() == Header::Physical && H.physical() == MainFile)
+   if (H.kind() == Header::Physical &&
+   (H.physical() == MainFile ||
+H.physical()->getLastRef().getDir().getName() ==
+ResourceDir)) {
  Satisfied = true;
+ continue;
+   }
for (const Include *I : Inc.match(H)) {
  Used.insert(I);
  Satisfied = true;
@@ -88,7 +95,8 @@
   AnalysisResults Results;
   for (const Include  : Inc.all()) {
 if (Used.contains() || !I.Resolved ||
-HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()))
+HeaderFilter(I.Resolved->getFileEntry().tryGetRealPathName()) ||
+I.Resolved->getDir().getName() == ResourceDir)
   continue;
 if (PI) {
   if (PI->shouldKeep(*I.Resolved))
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -574,6 +574,29 @@
   EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
 }
 
+TEST(IncludeCleaner, ResourceDirIsIgnored) {
+  auto TU = TestTU::withCode(R"cpp(
+#include "resources/amintrin.h"
+#include "resources/imintrin.h"
+void baz() {
+  bar();
+}
+  )cpp");
+  TU.ExtraArgs.push_back("-resource-dir");
+  TU.ExtraArgs.push_back(testPath("resources"));
+  TU.AdditionalFiles["resources/amintrin.h"] = "";
+   TU.AdditionalFiles["resources/imintrin.h"] = guard(R"cpp(
+#include "emintrin.h"
+  )cpp");
+  TU.AdditionalFiles["resources/emintrin.h"] = guard(R"cpp(
+void bar();
+  )cpp");
+  auto AST = TU.build();
+  auto Findings = computeIncludeCleanerFindings(AST);
+  EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
+  EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: 

[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-09 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc3ad4b763602: [include-cleaner] Follow `IWYU pragma: export` 
links transitively. (authored by VitaNuo).

Changed prior to commit:
  https://reviews.llvm.org/D157395?vs=548546=548571#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157395/new/

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -481,6 +481,55 @@
   ElementsAre(physicalHeader("bar.h"), physicalHeader("foo.h")));
 }
 
+TEST_F(HeadersForSymbolTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = guard(R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["export2.h"] = guard(R"cpp(
+#include "foo.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(
+struct foo {};
+  )cpp");
+  buildAST();
+  EXPECT_THAT(headersForFoo(),
+  ElementsAre(physicalHeader("foo.h"), physicalHeader("export1.h"),
+  physicalHeader("export2.h")));
+}
+
+TEST_F(HeadersForSymbolTest, IWYUTransitiveExportWithPrivate) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+void bar() { foo();}
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public1.h"
+#include "export2.h" // IWYU pragma: export
+void foo();
+  )cpp");
+  Inputs.ExtraFiles["export2.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public2.h"
+#include "export3.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["export3.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public3.h"
+#include "foo.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(
+void foo();
+  )cpp");
+  buildAST();
+  EXPECT_THAT(headersForFoo(),
+  ElementsAre(physicalHeader("foo.h"),
+   Header(StringRef("\"public1.h\"")),
+   physicalHeader("export1.h"),
+   physicalHeader("export2.h"),
+   physicalHeader("export3.h")));
+}
+
 TEST_F(HeadersForSymbolTest, AmbiguousStdSymbols) {
   struct {
 llvm::StringRef Code;
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -25,6 +25,8 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include 
+#include 
+#include 
 #include 
 
 namespace clang::include_cleaner {
@@ -188,13 +190,13 @@
 if (!PI)
   return {{FE, Hints::PublicHeader | Hints::OriginHeader}};
 bool IsOrigin = true;
+std::queue Exporters;
 while (FE) {
   Results.emplace_back(FE,
isPublicHeader(FE, *PI) |
(IsOrigin ? Hints::OriginHeader : Hints::None));
-  // FIXME: compute transitive exporter headers.
   for (const auto *Export : PI->getExporters(FE, SM.getFileManager()))
-Results.emplace_back(Export, isPublicHeader(Export, *PI));
+Exporters.push(Export);
 
   if (auto Verbatim = PI->getPublic(FE); 

[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-09 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 548546.
VitaNuo added a comment.

Add a test with private headers involved. Order of exporters is from outermost 
to innermost.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157395/new/

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -465,6 +465,55 @@
physicalHeader("foo.h")));
 }
 
+TEST_F(HeadersForSymbolTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = guard(R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["export2.h"] = guard(R"cpp(
+#include "foo.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(
+struct foo {};
+  )cpp");
+  buildAST();
+  EXPECT_THAT(headersForFoo(),
+  ElementsAre(physicalHeader("foo.h"), physicalHeader("export1.h"),
+  physicalHeader("export2.h")));
+}
+
+TEST_F(HeadersForSymbolTest, IWYUTransitiveExportWithPrivate) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+void bar() { foo();}
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public1.h"
+#include "export2.h" // IWYU pragma: export
+void foo();
+  )cpp");
+  Inputs.ExtraFiles["export2.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public2.h"
+#include "export3.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["export3.h"] = guard(R"cpp(
+// IWYU pragma: private, include "public3.h"
+#include "foo.h" // IWYU pragma: export
+  )cpp");
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(
+void foo();
+  )cpp");
+  buildAST();
+  EXPECT_THAT(headersForFoo(),
+  ElementsAre(physicalHeader("foo.h"),
+   Header(StringRef("\"public1.h\"")),
+   physicalHeader("export1.h"),
+   physicalHeader("export2.h"),
+   physicalHeader("export3.h")));
+}
+
 TEST_F(HeadersForSymbolTest, AmbiguousStdSymbols) {
   struct {
 llvm::StringRef Code;
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -25,6 +25,8 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include 
+#include 
+#include 
 #include 
 
 namespace clang::include_cleaner {
@@ -188,13 +190,13 @@
 if (!PI)
   return {{FE, Hints::PublicHeader | Hints::OriginHeader}};
 bool IsOrigin = true;
+std::queue Exporters;
 while (FE) {
   Results.emplace_back(FE,
isPublicHeader(FE, *PI) |
(IsOrigin ? Hints::OriginHeader : Hints::None));
-  // FIXME: compute transitive exporter headers.
   for (const auto *Export : PI->getExporters(FE, SM.getFileManager()))
-Results.emplace_back(Export, isPublicHeader(Export, *PI));
+Exporters.push(Export);
 
   if (auto Verbatim = PI->getPublic(FE); !Verbatim.empty()) {
 Results.emplace_back(Verbatim,
@@ -209,6 +211,20 @@
   FE = SM.getFileEntryForID(FID);
   IsOrigin = false;
 }
+// Now traverse provider trees 

[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-09 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 548540.
VitaNuo added a comment.

Address the comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157395/new/

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -465,6 +465,25 @@
physicalHeader("foo.h")));
 }
 
+TEST_F(HeadersForSymbolTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["export2.h"] = R"cpp(
+#include "foo.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(
+struct foo {};
+  )cpp");
+  buildAST();
+  EXPECT_THAT(headersForFoo(),
+  ElementsAre(physicalHeader("foo.h"), physicalHeader("export1.h"),
+  physicalHeader("export2.h")));
+}
+
 TEST_F(HeadersForSymbolTest, AmbiguousStdSymbols) {
   struct {
 llvm::StringRef Code;
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -25,6 +25,8 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include 
+#include 
+#include 
 #include 
 
 namespace clang::include_cleaner {
@@ -188,13 +190,13 @@
 if (!PI)
   return {{FE, Hints::PublicHeader | Hints::OriginHeader}};
 bool IsOrigin = true;
+std::queue Exporters;
 while (FE) {
   Results.emplace_back(FE,
isPublicHeader(FE, *PI) |
(IsOrigin ? Hints::OriginHeader : Hints::None));
-  // FIXME: compute transitive exporter headers.
   for (const auto *Export : PI->getExporters(FE, SM.getFileManager()))
-Results.emplace_back(Export, isPublicHeader(Export, *PI));
+Exporters.push(Export);
 
   if (auto Verbatim = PI->getPublic(FE); !Verbatim.empty()) {
 Results.emplace_back(Verbatim,
@@ -209,6 +211,20 @@
   FE = SM.getFileEntryForID(FID);
   IsOrigin = false;
 }
+// Now traverse provider trees rooted at exporters.
+// Note that we only traverse export edges, and ignore private -> public
+// mappings, as those pragmas apply to exporter, and not the main provider
+// being exported in this header.
+std::set SeenExports;
+while (!Exporters.empty()) {
+  auto *Export = Exporters.front();
+  Exporters.pop();
+  if (!SeenExports.insert(Export).second) // In case of cyclic exports
+continue;
+  Results.emplace_back(Export, isPublicHeader(Export, *PI));
+  for (const auto *Export : PI->getExporters(Export, SM.getFileManager()))
+Exporters.push(Export);
+}
 return Results;
   }
   case SymbolLocation::Standard: {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157400: [include-cleaner] Dont boost private headers beyond public ones

2023-08-08 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

Thanks.




Comment at: clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp:474
+  Inputs.ExtraFiles["bar.h"] =
+  guard(R"cpp(#include "foo.h" // IWYU pragma: export)cpp");
+  Inputs.ExtraFiles["foo.h"] = guard(R"cpp(

nit: would be better to split this into multiple lines.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157400/new/

https://reviews.llvm.org/D157400

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-08 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 548217.
VitaNuo added a comment.

Remove the preferred header hint for exporters.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157395/new/

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -439,6 +439,25 @@
   PI.getExporters(SM.getFileEntryForID(SM.getMainFileID()), FM).empty());
 }
 
+TEST_F(PragmaIncludeTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["export2.h"] = R"cpp(
+#include "provider.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["provider.h"] = "";
+  TestAST Processed = build();
+  auto  = Processed.fileManager();
+
+  EXPECT_THAT(PI.getExporters(FM.getFile("provider.h").get(), FM),
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("export2.h")));
+}
+
 TEST_F(PragmaIncludeTest, IWYUExportForStandardHeaders) {
   Inputs.Code = R"cpp(
 #include "export.h"
@@ -484,9 +503,11 @@
   testing::UnorderedElementsAre(FileNamed("export1.h"),
 FileNamed("normal.h")));
   EXPECT_THAT(PI.getExporters(FM.getFile("private2.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("normal.h")));
   EXPECT_THAT(PI.getExporters(FM.getFile("private3.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("normal.h")));
 
   EXPECT_TRUE(PI.getExporters(FM.getFile("foo.h").get(), FM).empty());
   EXPECT_TRUE(PI.getExporters(FM.getFile("bar.h").get(), FM).empty());
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -387,24 +393,44 @@
   return It->getSecond();
 }
 
+template 
 static llvm::SmallVector
-toFileEntries(llvm::ArrayRef FileNames, FileManager ) {
+toFileEntries(Iter FileNamesBegin, Iter FileNamesEnd, FileManager ) {
   llvm::SmallVector Results;
 
-  for (auto FName : FileNames) {
+  for (auto FNameIt = FileNamesBegin; FNameIt != FileNamesEnd; ++FNameIt) {
 // FIMXE: log the failing cases?
-if (auto FE = expectedToOptional(FM.getFileRef(FName)))
+if (auto FE = expectedToOptional(FM.getFileRef(*FNameIt)))
   Results.push_back(*FE);
   }
   return Results;
 }
-llvm::SmallVector
-PragmaIncludes::getExporters(const FileEntry *File, FileManager ) const {
-  auto It = IWYUExportBy.find(File->getUniqueID());
+
+void collectExportersRecursively(
+llvm::DenseMap>
+IWYUExportBy,
+const llvm::sys::fs::UniqueID , std::set ,
+FileManager ) {
+  auto It = IWYUExportBy.find(UID);
   if (It == IWYUExportBy.end())
-return {};
+return;
+  auto Exporters =
+  toFileEntries(It->getSecond().begin(), It->getSecond().end(), FM);
+  for (const auto  : Exporters) {
+Result.insert(E);
+collectExportersRecursively(IWYUExportBy, E->getUniqueID(), Result, FM);
+  }
+}
 
-  return toFileEntries(It->getSecond(), FM);
+llvm::SmallVector
+PragmaIncludes::getExporters(const FileEntry *File, FileManager ) const {
+  std::set UniqueExporters;
+  collectExportersRecursively(IWYUExportBy, File->getUniqueID(),
+ 

[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-08 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 548204.
VitaNuo added a comment.

Remove accidental comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157395/new/

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -439,6 +439,25 @@
   PI.getExporters(SM.getFileEntryForID(SM.getMainFileID()), FM).empty());
 }
 
+TEST_F(PragmaIncludeTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["export2.h"] = R"cpp(
+#include "provider.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["provider.h"] = "";
+  TestAST Processed = build();
+  auto  = Processed.fileManager();
+
+  EXPECT_THAT(PI.getExporters(FM.getFile("provider.h").get(), FM),
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("export2.h")));
+}
+
 TEST_F(PragmaIncludeTest, IWYUExportForStandardHeaders) {
   Inputs.Code = R"cpp(
 #include "export.h"
@@ -484,9 +503,11 @@
   testing::UnorderedElementsAre(FileNamed("export1.h"),
 FileNamed("normal.h")));
   EXPECT_THAT(PI.getExporters(FM.getFile("private2.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("normal.h")));
   EXPECT_THAT(PI.getExporters(FM.getFile("private3.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("normal.h")));
 
   EXPECT_TRUE(PI.getExporters(FM.getFile("foo.h").get(), FM).empty());
   EXPECT_TRUE(PI.getExporters(FM.getFile("bar.h").get(), FM).empty());
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -387,24 +393,44 @@
   return It->getSecond();
 }
 
+template 
 static llvm::SmallVector
-toFileEntries(llvm::ArrayRef FileNames, FileManager ) {
+toFileEntries(Iter FileNamesBegin, Iter FileNamesEnd, FileManager ) {
   llvm::SmallVector Results;
 
-  for (auto FName : FileNames) {
+  for (auto FNameIt = FileNamesBegin; FNameIt != FileNamesEnd; ++FNameIt) {
 // FIMXE: log the failing cases?
-if (auto FE = expectedToOptional(FM.getFileRef(FName)))
+if (auto FE = expectedToOptional(FM.getFileRef(*FNameIt)))
   Results.push_back(*FE);
   }
   return Results;
 }
-llvm::SmallVector
-PragmaIncludes::getExporters(const FileEntry *File, FileManager ) const {
-  auto It = IWYUExportBy.find(File->getUniqueID());
+
+void collectExportersRecursively(
+llvm::DenseMap>
+IWYUExportBy,
+const llvm::sys::fs::UniqueID , std::set ,
+FileManager ) {
+  auto It = IWYUExportBy.find(UID);
   if (It == IWYUExportBy.end())
-return {};
+return;
+  auto Exporters =
+  toFileEntries(It->getSecond().begin(), It->getSecond().end(), FM);
+  for (const auto  : Exporters) {
+Result.insert(E);
+collectExportersRecursively(IWYUExportBy, E->getUniqueID(), Result, FM);
+  }
+}
 
-  return toFileEntries(It->getSecond(), FM);
+llvm::SmallVector
+PragmaIncludes::getExporters(const FileEntry *File, FileManager ) const {
+  std::set UniqueExporters;
+  collectExportersRecursively(IWYUExportBy, File->getUniqueID(),
+  UniqueExporters, FM);
+  llvm::SmallVector 

[PATCH] D157395: [include-cleaner] Follow `IWYU pragma: export` links transitively.

2023-08-08 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added a subscriber: kadircet.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157395

Files:
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/include-cleaner/unittests/RecordTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
===
--- clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -439,6 +439,25 @@
   PI.getExporters(SM.getFileEntryForID(SM.getMainFileID()), FM).empty());
 }
 
+TEST_F(PragmaIncludeTest, IWYUTransitiveExport) {
+  Inputs.Code = R"cpp(
+#include "export1.h"
+  )cpp";
+  Inputs.ExtraFiles["export1.h"] = R"cpp(
+#include "export2.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["export2.h"] = R"cpp(
+#include "provider.h" // IWYU pragma: export
+  )cpp";
+  Inputs.ExtraFiles["provider.h"] = "";
+  TestAST Processed = build();
+  auto  = Processed.fileManager();
+
+  EXPECT_THAT(PI.getExporters(FM.getFile("provider.h").get(), FM),
+  testing::UnorderedElementsAre(FileNamed("export1.h"),
+FileNamed("export2.h")));
+}
+
 TEST_F(PragmaIncludeTest, IWYUExportForStandardHeaders) {
   Inputs.Code = R"cpp(
 #include "export.h"
@@ -483,13 +502,13 @@
   EXPECT_THAT(PI.getExporters(FM.getFile("private1.h").get(), FM),
   testing::UnorderedElementsAre(FileNamed("export1.h"),
 FileNamed("normal.h")));
-  EXPECT_THAT(PI.getExporters(FM.getFile("private2.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
-  EXPECT_THAT(PI.getExporters(FM.getFile("private3.h").get(), FM),
-  testing::UnorderedElementsAre(FileNamed("export1.h")));
+  // EXPECT_THAT(PI.getExporters(FM.getFile("private2.h").get(), FM),
+  // testing::UnorderedElementsAre(FileNamed("export1.h")));
+  // EXPECT_THAT(PI.getExporters(FM.getFile("private3.h").get(), FM),
+  // testing::UnorderedElementsAre(FileNamed("export1.h")));
 
-  EXPECT_TRUE(PI.getExporters(FM.getFile("foo.h").get(), FM).empty());
-  EXPECT_TRUE(PI.getExporters(FM.getFile("bar.h").get(), FM).empty());
+  // EXPECT_TRUE(PI.getExporters(FM.getFile("foo.h").get(), FM).empty());
+  // EXPECT_TRUE(PI.getExporters(FM.getFile("bar.h").get(), FM).empty());
 }
 
 TEST_F(PragmaIncludeTest, SelfContained) {
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclGroup.h"
 #include "clang/Basic/FileEntry.h"
+#include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,16 +25,21 @@
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
 #include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileSystem/UniqueID.h"
 #include "llvm/Support/StringSaver.h"
 #include 
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 
@@ -387,24 +393,44 @@
   return It->getSecond();
 }
 
+template 
 static llvm::SmallVector
-toFileEntries(llvm::ArrayRef FileNames, FileManager ) {
+toFileEntries(Iter FileNamesBegin, Iter FileNamesEnd, FileManager ) {
   llvm::SmallVector Results;
 
-  for (auto FName : FileNames) {
+  for (auto FNameIt = FileNamesBegin; FNameIt != FileNamesEnd; ++FNameIt) {
 // FIMXE: log the failing cases?
-if (auto FE = expectedToOptional(FM.getFileRef(FName)))
+if (auto FE = expectedToOptional(FM.getFileRef(*FNameIt)))
   Results.push_back(*FE);
   }
   return Results;
 }
-llvm::SmallVector
-PragmaIncludes::getExporters(const FileEntry *File, FileManager ) const {
-  auto It = IWYUExportBy.find(File->getUniqueID());
+
+void collectExportersRecursively(
+llvm::DenseMap>
+IWYUExportBy,
+const llvm::sys::fs::UniqueID , std::set ,
+FileManager ) {
+  auto It = IWYUExportBy.find(UID);
   if (It == IWYUExportBy.end())
-return {};
+return;
+  auto Exporters =
+  toFileEntries(It->getSecond().begin(), It->getSecond().end(), FM);
+  for (const auto  : Exporters) {
+Result.insert(E);
+collectExportersRecursively(IWYUExportBy, E->getUniqueID(), Result, FM);
+  }
+}
 
-  

[PATCH] D157390: [clang-tidy][include-cleaner] Add option to control deduplication of findings per symbol

2023-08-08 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

Thanks.




Comment at: clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.h:48
+  // Whether emit only one finding per usage of a symbol.
+  const bool EmitOnce;
   llvm::SmallVector IgnoreHeadersRegex;

Nit: let's name this similar or same to the option, i.e., `const bool 
DeduplicateFindings`. It might be easier to digest.



Comment at: clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp:150
+  "no header providing \"bar\" is directly included");
+EXPECT_EQ(Errors.back().Message.Message,
+  "no header providing \"bar\" is directly included");

Nit: check that these are actually different findings (i.e., stemming from 
different symbol refs).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157390/new/

https://reviews.llvm.org/D157390

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157256: [clangd][clang-tidy][std_symbol_map] Add missing symbol.

2023-08-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8a5c0ccee293: [clangd][clang-tidy][std_symbol_map] Add 
missing symbol. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157256/new/

https://reviews.llvm.org/D157256

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -363,6 +363,7 @@
 // Symbols missing from the generated symbol map as reported by users.
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
+SYMBOL(any_cast, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -363,6 +363,7 @@
 // Symbols missing from the generated symbol map as reported by users.
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
+SYMBOL(any_cast, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157256: [clangd][clang-tidy][std_symbol_map] Add missing symbol.

2023-08-07 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
VitaNuo added a reviewer: kadircet.
Herald added subscribers: carlosgalvezp, arphaman, xazax.hun.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157256

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -363,6 +363,7 @@
 // Symbols missing from the generated symbol map as reported by users.
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
+SYMBOL(any_cast, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -363,6 +363,7 @@
 // Symbols missing from the generated symbol map as reported by users.
 // Remove when the generator starts producing them.
 SYMBOL(make_any, std::, )
+SYMBOL(any_cast, std::, )
 
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157093: [clangd][clang-tidy][stdlib] Add a missing symbol to the mapping.

2023-08-04 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5d492766a8fb: [clangd][clang-tidy][stdlib] Add a missing 
symbol to the mapping. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157093/new/

https://reviews.llvm.org/D157093

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -360,6 +360,10 @@
 SYMBOL(make_index_sequence, std::, )
 SYMBOL(make_integer_sequence, std::, )
 
+// Symbols missing from the generated symbol map as reported by users.
+// Remove when the generator starts producing them.
+SYMBOL(make_any, std::, )
+
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"
 // text, which are not handled by the script.


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -360,6 +360,10 @@
 SYMBOL(make_index_sequence, std::, )
 SYMBOL(make_integer_sequence, std::, )
 
+// Symbols missing from the generated symbol map as reported by users.
+// Remove when the generator starts producing them.
+SYMBOL(make_any, std::, )
+
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
 // text, which are not handled by the script.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157093: [clangd][clang-tidy][stdlib] Add a missing symbol to the mapping.

2023-08-04 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: carlosgalvezp, kadircet, arphaman, xazax.hun.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157093

Files:
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -360,6 +360,10 @@
 SYMBOL(make_index_sequence, std::, )
 SYMBOL(make_integer_sequence, std::, )
 
+// Symbols missing from the generated symbol map as reported by users.
+// Remove when the generator starts producing them.
+SYMBOL(make_any, std::, )
+
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, 
..., _N"
 // text, which are not handled by the script.


Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -360,6 +360,10 @@
 SYMBOL(make_index_sequence, std::, )
 SYMBOL(make_integer_sequence, std::, )
 
+// Symbols missing from the generated symbol map as reported by users.
+// Remove when the generator starts producing them.
+SYMBOL(make_any, std::, )
+
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
 // text, which are not handled by the script.
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156123: [include-cleaner] Unify always_keep with rest of the keep logic

2023-08-01 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156123/new/

https://reviews.llvm.org/D156123

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156122: [include-cleaner] Introduce support for always_keep pragma

2023-08-01 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156122/new/

https://reviews.llvm.org/D156122

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156123: [include-cleaner] Unify always_keep with rest of the keep logic

2023-07-31 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added inline comments.



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:248
+  if (Top.SeenAtFile == SM.getMainFileID() && IncludedFile)
+Out->ShouldKeep.insert(IncludedFile->getUniqueID());
 }

If I understand correctly, you should be able to extract `IncludedFile` from 
`IncludedHeader->physical()` and then you don't need the extra parameter.

Also, technically this and the below code used to work for standard and 
verbatim headers before this change. Now it probably won't, since there will be 
no corresponding `IncludedFile`. Is this actually something to worry about?



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:262
+  Out->ShouldKeep.insert(IncludedFile->getUniqueID());
+}
 

nit: remove braces.



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:419
 bool PragmaIncludes::shouldKeep(const FileEntry *FE) const {
-  return AlwaysKeep.contains(FE->getUniqueID());
+  return ShouldKeep.contains(FE->getUniqueID());
 }

Consider inlining this function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156123/new/

https://reviews.llvm.org/D156123

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156122: [include-cleaner] Introduce support for always_keep pragma

2023-07-31 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added inline comments.



Comment at: 
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h:63
+  bool shouldKeep(unsigned HashLineNumber) const;
+  bool shouldKeep(const FileEntry *FE) const;
 

Why wouldn't you actually inline the implementation for both these functions? 
The implementations seem trivial enough. 



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:37
 #include 
+#include 
 

I don't think you've added a dependency on all these headers in this patch. 
Nice if you've actually fixed the missing include violations all over the file, 
but please double-check that there are no debugging etc. artefacts left.



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:272
 
+FileID CommentFID = SM.getFileID(Range.getBegin());
+auto *FE = SM.getFileEntryForID(CommentFID);

You might want to inline this call, it seems to have a single usage right below.



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:288
+if (Pragma->consume_front("always_keep")) {
+  Out->AlwaysKeep.insert(FE->getUniqueID());
+  return false;

I believe you need to check `FE` for `nullptr`, similar to private pragma 
handling above. 
Also, the code above does `FE->getLastRef().getUniqueID()`, is there any 
difference to `FE->getUniqueID()`? I wouldn't expect so, but then you could 
maybe unify this one with the above, this way or the other.



Comment at: clang-tools-extra/include-cleaner/lib/Record.cpp:421
+  return AlwaysKeep.contains(FE->getUniqueID());
+}
+

As mentioned above, consider inlining.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156122/new/

https://reviews.llvm.org/D156122

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156659: [clangd] Rollforward include-cleaner library usage in symbol collector.

2023-07-31 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added projects: clang, clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156659

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/IndexActionTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
  clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc

Index: clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
===
--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
+++ clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc
@@ -360,6 +360,11 @@
 SYMBOL(make_index_sequence, std::, )
 SYMBOL(make_integer_sequence, std::, )
 
+// generated symbol map is missing these symbols.
+// Remove them when the cause(s) are identified.
+SYMBOL(div, std::, )
+SYMBOL(abort, std::, )
+
 // The std::placeholder symbols (_1, ..., _N) are listed in the cppreference
 // placeholder.html, but the index only contains a single entry with "_1, _2, ..., _N"
 // text, which are not handled by the script.
Index: clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
===
--- clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
+++ clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
@@ -123,7 +123,7 @@
 if (FD->getNumParams() == 1)
   // move(T&& t)
   return tooling::stdlib::Header::named("");
-if (FD->getNumParams() == 3)
+if (FD->getNumParams() == 3 || FD->getNumParams() == 4)
   // move(InputIt first, InputIt last, OutputIt dest);
   return tooling::stdlib::Header::named("");
   } else if (FName == "remove") {
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -14,6 +14,7 @@
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexingOptions.h"
@@ -1554,6 +1555,8 @@
 // Move overloads have special handling.
 template  T&& move(_T&& __value);
 template  _O move(_I, _I, _O);
+template  _O move(
+  _T&&, _O, _O, _I);
   }
   )cpp",
   /*Main=*/"");
@@ -1565,7 +1568,8 @@
 includeHeader("")),
   // Parameter names are demangled.
   AllOf(labeled("move(T &)"), includeHeader("")),
-  AllOf(labeled("move(I, I, O)"), includeHeader("";
+  AllOf(labeled("move(I, I, O)"), includeHeader("")),
+  AllOf(labeled("move(T &&, O, O, I)"), includeHeader("";
 }
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
@@ -1592,7 +1596,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
@@ -1978,6 +1982,24 @@
   qName("A"), hasKind(clang::index::SymbolKind::Concept;
 }
 
+TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once 
+struct Foo;
+#include "full.h"
+)cpp";
+  auto FullFile = testPath("full.h");
+  InMemoryFileSystem->addFile(FullFile, 0,
+  llvm::MemoryBuffer::getMemBuffer(R"cpp(
+#pragma once 
+struct Foo {};)cpp"));
+  runSymbolCollector(Header, /*Main=*/"",
+ /*ExtraArgs=*/{"-I", testRoot()});
+  EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+   qName("Foo"),
+   includeHeader(URI::create(FullFile).toString()
+  << *Symbols.begin();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/unittests/IndexActionTests.cpp
===
--- clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -8,11 +8,15 @@
 
 #include "Headers.h"
 #include "TestFS.h"
+#include "URI.h"
 #include "index/IndexAction.h"
 #include "index/Serialization.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include 

[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the comments!




Comment at: clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp:62
+  const auto  = Inputs.AST->getSourceManager();
+  std::set Spellings;
+  for (const auto  : Findings.MissingIncludes)

kadircet wrote:
> prefer `llvm::StringSet<>`
I think it should actually be `llvm::DenseSet`. We 
should first de-duplicate providers and only then spell the resulting headers. 
In this version, we were potentially spelling the same provider multiple times.



Comment at: clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp:64-67
+for (const auto  : Missing.Providers)
+  Spellings.insert(include_cleaner::spellHeader(
+  {P, Inputs.AST->getPreprocessor().getHeaderSearchInfo(),
+   SM.getFileEntryForID(SM.getMainFileID())}));

kadircet wrote:
> this will insert all providers for a symbol (e.g. if we have both `foo.h` and 
> `bar.h`, we'll insert both).
you're right, thanks


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 544751.
VitaNuo marked 8 inline comments as done.
VitaNuo added a comment.

Address review comments. Update the action to trigger when source action is 
requested explicitly or from the preamble range. Add unit test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153769/new/

https://reviews.llvm.org/D153769

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
  clang-tools-extra/clangd/test/request-reply.test
  clang-tools-extra/clangd/test/tweaks-format.test
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CMakeLists.txt
  clang-tools-extra/clangd/unittests/ClangdTests.cpp
  clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
  clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp

Index: clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
===
--- clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/TweakTesting.cpp
@@ -8,6 +8,7 @@
 
 #include "TweakTesting.h"
 
+#include "Protocol.h"
 #include "SourceCode.h"
 #include "TestTU.h"
 #include "refactor/Tweak.h"
@@ -65,18 +66,19 @@
 applyTweak(ParsedAST , llvm::Annotations::Range Range, StringRef TweakID,
const SymbolIndex *Index, llvm::vfs::FileSystem *FS) {
   std::optional> Result;
-  SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Range.Begin,
-Range.End, [&](SelectionTree ST) {
-  Tweak::Selection S(Index, AST, Range.Begin,
- Range.End, std::move(ST), FS);
-  if (auto T = prepareTweak(TweakID, S, nullptr)) {
-Result = (*T)->apply(S);
-return true;
-  } else {
-llvm::consumeError(T.takeError());
-return false;
-  }
-});
+  SelectionTree::createEach(
+  AST.getASTContext(), AST.getTokens(), Range.Begin, Range.End,
+  [&](SelectionTree ST) {
+Tweak::Selection S(Index, AST, Range.Begin, Range.End, std::move(ST),
+   FS, {std::string{CodeAction::SOURCE_KIND}});
+if (auto T = prepareTweak(TweakID, S, nullptr)) {
+  Result = (*T)->apply(S);
+  return true;
+} else {
+  llvm::consumeError(T.takeError());
+  return false;
+}
+  });
   return Result;
 }
 
Index: clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
===
--- /dev/null
+++ clang-tools-extra/clangd/unittests/tweaks/OrganizeImportsTests.cpp
@@ -0,0 +1,84 @@
+//===--OrganizeImportsTest.cpp --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "TweakTesting.h"
+#include "llvm/ADT/StringRef.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+TWEAK_TEST(OrganizeImports);
+
+TEST_F(OrganizeImportsTest, All) {
+  Header = "void foo();";
+  struct {
+llvm::StringRef Code;
+llvm::StringRef Header;
+llvm::StringRef Expected;
+  } Cases[] = {{// Remove unused include.
+R"cpp(
+#include "TestTU.h"
+void foo() {}
+void b^ar() {
+foo();
+}
+)cpp",
+R"cpp(
+#pragma once
+)cpp",
+R"cpp(
+void foo() {}
+void bar() {
+foo();
+}
+)cpp"},
+   {// Add missing include.
+R"cpp(
+void b^ar() {
+foo();
+}
+)cpp",
+R"cpp(
+#pragma once
+void foo();
+)cpp",
+R"cpp(
+#include "TestTU.h"
+void bar() {
+foo();
+}
+)cpp"},
+   {// Replace unused include with missing.
+R"cpp(
+#include "foo.h"
+void b^ar() {
+foo();
+}
+)cpp",
+R"cpp(
+#pragma once
+void foo();
+)cpp",
+R"cpp(
+#include "TestTU.h"
+void bar() {
+foo();
+}
+)cpp"}};
+  for (auto C : Cases) {
+

[PATCH] D156403: [clangd] Revert the symbol collector behavior to old pre-include-cleaner-library behavior due to a regression.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3c6a7b0045af: [clangd] Revert the symbol collector behavior 
to old pre-include-cleaner… (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156403/new/

https://reviews.llvm.org/D156403

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1592,7 +1592,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
Index: clang-tools-extra/clangd/unittests/FileIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -239,7 +239,7 @@
   "");
 }
 
-TEST(FileIndexTest, IWYUPragmaExport) {
+TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
   FileIndex M;
 
   TestTU File;
Index: clang-tools-extra/clangd/index/SymbolCollector.h
===
--- clang-tools-extra/clangd/index/SymbolCollector.h
+++ clang-tools-extra/clangd/index/SymbolCollector.h
@@ -175,11 +175,6 @@
   void setIncludeLocation(const Symbol , SourceLocation,
   const include_cleaner::Symbol );
 
-  // Providers for Symbol.IncludeHeaders.
-  // The final spelling is calculated in finish().
-  llvm::DenseMap>
-  SymbolProviders;
-
   // Files which contain ObjC symbols.
   // This is finalized and used in finish().
   llvm::DenseSet FilesWithObjCConstructs;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -36,6 +36,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
@@ -836,14 +837,25 @@
   // Use the expansion location to get the #include header since this is
   // where the symbol is exposed.
   IncludeFiles[S.ID] = SM.getDecomposedExpansionLoc(DefLoc).first;
+}
 
-  auto [It, Inserted] = SymbolProviders.try_emplace(S.ID);
-  if (Inserted) {
-auto Headers =
-include_cleaner::headersForSymbol(Sym, SM, Opts.PragmaIncludes);
-if (!Headers.empty())
-  It->second = Headers.front();
-  }
+llvm::StringRef getStdHeader(const Symbol *S, const LangOptions ) {
+  tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
+if (LangOpts.C11)
+  Lang = tooling::stdlib::Lang::C;
+else if(!LangOpts.CPlusPlus)
+  return "";
+
+if (S->Scope == "std::" && S->Name == "move") {
+  if (!S->Signature.contains(','))
+return "";
+  return "";
+}
+   
+if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
+ if (auto Header = StdSym->header())
+   return Header->name();
+return "";
 }
 
 void SymbolCollector::finish() {
@@ -869,16 +881,13 @@
 }
   }
   llvm::DenseMap FileToContainsImportsOrObjC;
-  llvm::DenseMap HeaderSpelling;
   // Fill in IncludeHeaders.
   // We delay this until end of TU so header guards are all resolved.
-  for (const auto &[SID, OptionalProvider] : SymbolProviders) {
+  for (const auto &[SID, FID] : IncludeFiles) {
 const Symbol *S = Symbols.find(SID);
 if (!S)
   continue;
-assert(IncludeFiles.find(SID) != IncludeFiles.end());
 
-const auto FID = IncludeFiles.at(SID);
 // Determine if the FID is #include'd or #import'ed.
 Symbol::IncludeDirective Directives = Symbol::Invalid;
 auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
@@ -898,54 +907,20 @@
 if (Directives == Symbol::Invalid)
   continue;
 
-// Use the include location-based logic for Objective-C symbols.
-if (Directives & Symbol::Import) {
-  if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
-  !IncludeHeader.empty()) {
-auto NewSym = *S;
-NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
-Symbols.insert(NewSym);
-  }
- 

[PATCH] D156403: [clangd] Revert the symbol collector behavior to old pre-include-cleaner-library behavior due to a regression.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 544689.
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Address comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156403/new/

https://reviews.llvm.org/D156403

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1592,7 +1592,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
Index: clang-tools-extra/clangd/unittests/FileIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -239,7 +239,7 @@
   "");
 }
 
-TEST(FileIndexTest, IWYUPragmaExport) {
+TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
   FileIndex M;
 
   TestTU File;
Index: clang-tools-extra/clangd/index/SymbolCollector.h
===
--- clang-tools-extra/clangd/index/SymbolCollector.h
+++ clang-tools-extra/clangd/index/SymbolCollector.h
@@ -175,11 +175,6 @@
   void setIncludeLocation(const Symbol , SourceLocation,
   const include_cleaner::Symbol );
 
-  // Providers for Symbol.IncludeHeaders.
-  // The final spelling is calculated in finish().
-  llvm::DenseMap>
-  SymbolProviders;
-
   // Files which contain ObjC symbols.
   // This is finalized and used in finish().
   llvm::DenseSet FilesWithObjCConstructs;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -36,6 +36,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
@@ -836,14 +837,25 @@
   // Use the expansion location to get the #include header since this is
   // where the symbol is exposed.
   IncludeFiles[S.ID] = SM.getDecomposedExpansionLoc(DefLoc).first;
+}
 
-  auto [It, Inserted] = SymbolProviders.try_emplace(S.ID);
-  if (Inserted) {
-auto Headers =
-include_cleaner::headersForSymbol(Sym, SM, Opts.PragmaIncludes);
-if (!Headers.empty())
-  It->second = Headers.front();
-  }
+llvm::StringRef getStdHeader(const Symbol *S, const LangOptions ) {
+  tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
+if (LangOpts.C11)
+  Lang = tooling::stdlib::Lang::C;
+else if(!LangOpts.CPlusPlus)
+  return "";
+
+if (S->Scope == "std::" && S->Name == "move") {
+  if (!S->Signature.contains(','))
+return "";
+  return "";
+}
+   
+if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
+ if (auto Header = StdSym->header())
+   return Header->name();
+return "";
 }
 
 void SymbolCollector::finish() {
@@ -869,16 +881,13 @@
 }
   }
   llvm::DenseMap FileToContainsImportsOrObjC;
-  llvm::DenseMap HeaderSpelling;
   // Fill in IncludeHeaders.
   // We delay this until end of TU so header guards are all resolved.
-  for (const auto &[SID, OptionalProvider] : SymbolProviders) {
+  for (const auto &[SID, FID] : IncludeFiles) {
 const Symbol *S = Symbols.find(SID);
 if (!S)
   continue;
-assert(IncludeFiles.find(SID) != IncludeFiles.end());
 
-const auto FID = IncludeFiles.at(SID);
 // Determine if the FID is #include'd or #import'ed.
 Symbol::IncludeDirective Directives = Symbol::Invalid;
 auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
@@ -898,54 +907,20 @@
 if (Directives == Symbol::Invalid)
   continue;
 
-// Use the include location-based logic for Objective-C symbols.
-if (Directives & Symbol::Import) {
-  if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
-  !IncludeHeader.empty()) {
-auto NewSym = *S;
-NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
-Symbols.insert(NewSym);
-  }
-  // FIXME: use providers from include-cleaner library once it's polished
-  // for Objective-C.
-  continue;
-}
-
-  

[PATCH] D156403: [clangd] Revert the symbol collector behavior to old pre-include-cleaner-library behavior due to a regression.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 544684.
VitaNuo marked 6 inline comments as done.
VitaNuo added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156403/new/

https://reviews.llvm.org/D156403

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1592,7 +1592,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
Index: clang-tools-extra/clangd/unittests/FileIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -239,7 +239,7 @@
   "");
 }
 
-TEST(FileIndexTest, IWYUPragmaExport) {
+TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
   FileIndex M;
 
   TestTU File;
Index: clang-tools-extra/clangd/index/SymbolCollector.h
===
--- clang-tools-extra/clangd/index/SymbolCollector.h
+++ clang-tools-extra/clangd/index/SymbolCollector.h
@@ -175,11 +175,6 @@
   void setIncludeLocation(const Symbol , SourceLocation,
   const include_cleaner::Symbol );
 
-  // Providers for Symbol.IncludeHeaders.
-  // The final spelling is calculated in finish().
-  llvm::DenseMap>
-  SymbolProviders;
-
   // Files which contain ObjC symbols.
   // This is finalized and used in finish().
   llvm::DenseSet FilesWithObjCConstructs;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -36,6 +36,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
@@ -836,14 +837,6 @@
   // Use the expansion location to get the #include header since this is
   // where the symbol is exposed.
   IncludeFiles[S.ID] = SM.getDecomposedExpansionLoc(DefLoc).first;
-
-  auto [It, Inserted] = SymbolProviders.try_emplace(S.ID);
-  if (Inserted) {
-auto Headers =
-include_cleaner::headersForSymbol(Sym, SM, Opts.PragmaIncludes);
-if (!Headers.empty())
-  It->second = Headers.front();
-  }
 }
 
 void SymbolCollector::finish() {
@@ -869,16 +862,13 @@
 }
   }
   llvm::DenseMap FileToContainsImportsOrObjC;
-  llvm::DenseMap HeaderSpelling;
   // Fill in IncludeHeaders.
   // We delay this until end of TU so header guards are all resolved.
-  for (const auto &[SID, OptionalProvider] : SymbolProviders) {
+  for (const auto &[SID, FID] : IncludeFiles) {
 const Symbol *S = Symbols.find(SID);
 if (!S)
   continue;
-assert(IncludeFiles.find(SID) != IncludeFiles.end());
 
-const auto FID = IncludeFiles.at(SID);
 // Determine if the FID is #include'd or #import'ed.
 Symbol::IncludeDirective Directives = Symbol::Invalid;
 auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
@@ -898,54 +888,32 @@
 if (Directives == Symbol::Invalid)
   continue;
 
-// Use the include location-based logic for Objective-C symbols.
-if (Directives & Symbol::Import) {
-  if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
-  !IncludeHeader.empty()) {
-auto NewSym = *S;
-NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
-Symbols.insert(NewSym);
-  }
-  // FIXME: use providers from include-cleaner library once it's polished
-  // for Objective-C.
-  continue;
+// FIXME: Use the include-cleaner library instead.
+tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
+if (ASTCtx->getLangOpts().C11)
+  Lang = tooling::stdlib::Lang::C;
+llvm::StringRef IncludeHeader;
+if (S->Scope == "std::" && S->Name == "move") {
+  IncludeHeader = "";
+  if (S->Signature.contains(','))
+IncludeHeader = "";
 }
+if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
+  if (auto Header = StdSym->header())
+IncludeHeader = Header->name();
 
-

[PATCH] D156403: [clangd] Revert the symbol collector behavior to old pre-include-cleaner-library behavior due to a regression.

2023-07-27 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156403

Files:
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1592,7 +1592,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
Index: clang-tools-extra/clangd/unittests/FileIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -239,7 +239,7 @@
   "");
 }
 
-TEST(FileIndexTest, IWYUPragmaExport) {
+TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
   FileIndex M;
 
   TestTU File;
Index: clang-tools-extra/clangd/index/SymbolCollector.cpp
===
--- clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -36,6 +36,7 @@
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Lex/Token.h"
 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
@@ -898,19 +899,39 @@
 if (Directives == Symbol::Invalid)
   continue;
 
-// Use the include location-based logic for Objective-C symbols.
-if (Directives & Symbol::Import) {
-  if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
-  !IncludeHeader.empty()) {
-auto NewSym = *S;
-NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
-Symbols.insert(NewSym);
-  }
-  // FIXME: use providers from include-cleaner library once it's polished
-  // for Objective-C.
+// FIXME: Remove the block below once we can rely on standard library
+// mapping inside the include-cleaner library again.
+llvm::StringRef IncludeHeader;
+if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name))
+  if (auto Header = StdSym->header())
+IncludeHeader = Header->name();
+if (S->Scope == "std::" && S->Name == "move") {
+  IncludeHeader = "";
+  if (S->Signature.contains(','))
+IncludeHeader = "";
+}
+if (!IncludeHeader.empty()) {
+  auto NewSym = *S;
+  NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
+  Symbols.insert(NewSym);
   continue;
 }
 
+// Use the old include location-based logic both for Objective-C and C++
+// symbols.
+if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
+!IncludeHeader.empty()) {
+  auto NewSym = *S;
+  NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
+  Symbols.insert(NewSym);
+}
+
+// FIXME: use providers from include-cleaner library once it's polished
+// for Objective-C.
+continue;
+
+// FIXME: Re-enable for C++ code. The code below uses include-cleaner
+// library but is currently unreachable due to regression.
 assert(Directives == Symbol::Include);
 // For #include's, use the providers computed by the include-cleaner
 // library.


Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1592,7 +1592,7 @@
  includeHeader("\"the/good/header.h\"";
 }
 
-TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
   CollectorOpts.CollectIncludePath = true;
   const std::string Header = R"cpp(#pragma once
 #include "exporter.h"
Index: clang-tools-extra/clangd/unittests/FileIndexTests.cpp
===
--- clang-tools-extra/clangd/unittests/FileIndexTests.cpp
+++ clang-tools-extra/clangd/unittests/FileIndexTests.cpp
@@ -239,7 +239,7 @@
   "");
 }
 
-TEST(FileIndexTest, IWYUPragmaExport) {
+TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
   FileIndex M;
 
   

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-07-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1f7c7d4bdd7b: [clangd] Update symbol collector to use 
include-cleaner. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/Preamble.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.h
  clang-tools-extra/clangd/index/FileIndex.cpp
  clang-tools-extra/clangd/index/FileIndex.h
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestWorkspace.cpp
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
===
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -46,7 +46,7 @@
 
 /// We consider a macro to be a different symbol each time it is defined.
 struct Macro {
-  IdentifierInfo *Name;
+  const IdentifierInfo *Name;
   /// The location of the Name where the macro is defined.
   SourceLocation Definition;
 
Index: clang-tools-extra/clangd/unittests/TestWorkspace.cpp
===
--- clang-tools-extra/clangd/unittests/TestWorkspace.cpp
+++ clang-tools-extra/clangd/unittests/TestWorkspace.cpp
@@ -7,8 +7,10 @@
 //===--===//
 
 #include "TestWorkspace.h"
+#include "clang-include-cleaner/Record.h"
 #include "index/FileIndex.h"
 #include "gtest/gtest.h"
+#include 
 #include 
 
 namespace clang {
@@ -21,14 +23,12 @@
   continue;
 TU.Code = Input.second.Code;
 TU.Filename = Input.first().str();
-TU.preamble(
-[&](CapturedASTCtx ASTCtx,
-const std::shared_ptr CanonIncludes) {
-  auto  = ASTCtx.getASTContext();
-  auto  = ASTCtx.getPreprocessor();
-  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
-*CanonIncludes);
-});
+TU.preamble([&](CapturedASTCtx ASTCtx,
+std::shared_ptr PI) {
+  auto  = ASTCtx.getASTContext();
+  auto  = ASTCtx.getPreprocessor();
+  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, *PI);
+});
 ParsedAST MainAST = TU.build();
 Index->updateMain(testPath(Input.first()), MainAST);
   }
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -162,9 +162,9 @@
 
 SymbolSlab TestTU::headerSymbols() const {
   auto AST = build();
-  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
-AST.getPreprocessor(),
-AST.getCanonicalIncludes()));
+  return std::get<0>(indexHeaderSymbols(
+  /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
+  *AST.getPragmaIncludes()));
 }
 
 RefSlab TestTU::headerRefs() const {
@@ -177,7 +177,7 @@
   auto Idx = std::make_unique();
   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
   AST.getASTContext(), AST.getPreprocessor(),
-  AST.getCanonicalIncludes());
+  *AST.getPragmaIncludes());
   Idx->updateMain(testPath(Filename), AST);
   return std::move(Idx);
 }
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -18,6 +18,7 @@
 #include "TUScheduler.h"
 #include "TestFS.h"
 #include "TestIndex.h"
+#include "clang-include-cleaner/Record.h"
 #include "support/Cancellation.h"
 #include 

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-07-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 542000.
VitaNuo marked 9 inline comments as done.
VitaNuo added a comment.

Address the latest comment batch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/Preamble.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.h
  clang-tools-extra/clangd/index/FileIndex.cpp
  clang-tools-extra/clangd/index/FileIndex.h
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestWorkspace.cpp
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
===
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -46,7 +46,7 @@
 
 /// We consider a macro to be a different symbol each time it is defined.
 struct Macro {
-  IdentifierInfo *Name;
+  const IdentifierInfo *Name;
   /// The location of the Name where the macro is defined.
   SourceLocation Definition;
 
Index: clang-tools-extra/clangd/unittests/TestWorkspace.cpp
===
--- clang-tools-extra/clangd/unittests/TestWorkspace.cpp
+++ clang-tools-extra/clangd/unittests/TestWorkspace.cpp
@@ -7,8 +7,10 @@
 //===--===//
 
 #include "TestWorkspace.h"
+#include "clang-include-cleaner/Record.h"
 #include "index/FileIndex.h"
 #include "gtest/gtest.h"
+#include 
 #include 
 
 namespace clang {
@@ -21,14 +23,12 @@
   continue;
 TU.Code = Input.second.Code;
 TU.Filename = Input.first().str();
-TU.preamble(
-[&](CapturedASTCtx ASTCtx,
-const std::shared_ptr CanonIncludes) {
-  auto  = ASTCtx.getASTContext();
-  auto  = ASTCtx.getPreprocessor();
-  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
-*CanonIncludes);
-});
+TU.preamble([&](CapturedASTCtx ASTCtx,
+std::shared_ptr PI) {
+  auto  = ASTCtx.getASTContext();
+  auto  = ASTCtx.getPreprocessor();
+  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, *PI);
+});
 ParsedAST MainAST = TU.build();
 Index->updateMain(testPath(Input.first()), MainAST);
   }
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -162,9 +162,9 @@
 
 SymbolSlab TestTU::headerSymbols() const {
   auto AST = build();
-  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
-AST.getPreprocessor(),
-AST.getCanonicalIncludes()));
+  return std::get<0>(indexHeaderSymbols(
+  /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
+  *AST.getPragmaIncludes()));
 }
 
 RefSlab TestTU::headerRefs() const {
@@ -177,7 +177,7 @@
   auto Idx = std::make_unique();
   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
   AST.getASTContext(), AST.getPreprocessor(),
-  AST.getCanonicalIncludes());
+  *AST.getPragmaIncludes());
   Idx->updateMain(testPath(Filename), AST);
   return std::move(Idx);
 }
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -18,6 +18,7 @@
 #include "TUScheduler.h"
 #include "TestFS.h"
 #include "TestIndex.h"
+#include "clang-include-cleaner/Record.h"
 #include "support/Cancellation.h"
 #include "support/Context.h"
 #include "support/Path.h"
@@ -1134,9 +1135,9 @@
   public:
 

[PATCH] D155614: [clangd] Remove redundant emptiness check in cross ref calculation for includes.

2023-07-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG22605f5f1bf8: [clangd] Make an include always refer to 
itself. Background: clang-review… (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155614/new/

https://reviews.llvm.org/D155614

Files:
  clang-tools-extra/clangd/XRefs.cpp


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1367,9 +1367,6 @@
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1367,9 +1367,6 @@
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155614: [clangd] Remove redundant emptiness check in cross ref calculation for includes.

2023-07-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added inline comments.
This revision is now accepted and ready to land.



Comment at: clang-tools-extra/clangd/XRefs.cpp:1361-1362
   });
-  if (Results.References.empty())
-return std::nullopt;
-

usaxena95 wrote:
> Sorry for the confusion. This looks intentional and somewhat valuable for 
> unused headers. We could fix this on clang-review's end as discussed on the 
> internal patch. We should also add a comment here explaining the rationale 
> behind returning no refs in such cases.
> 
> (The check below certainly looks redundant though and could be removed).
Yeah you're right the second check is redundant (probably an artefact of some 
unfinished refactoring). I will land this patch with the redundant check 
removed.



Comment at: clang-tools-extra/clangd/XRefs.cpp:1365
   // Add the #include line to the references list.
   ReferencesResult::Reference Result;
   Result.Loc.range =

kadircet wrote:
> i guess this patch is not needed anymore, but one comment about this 
> behaviour in clangd as well (no action needed in this patch)
> 
> why are we providing the reference to the include line itself? in other 
> interactions we do that for the sake of completeness (e.g. if you trigger 
> xrefs on a reference, seeing the declaration/definition location is useful) 
> but usually the result under the cursor is not so interesting, especially in 
> this case. the include line itself is drastically different than the nature 
> of other references, the user can only see the include, if they triggered the 
> xrefs on the include line itself and not when they trigger it on the symbol. 
> also that ref doesn't count if there's no symbols used?
> 
> FWIW I am not sure if that's a useful interaction. What was the rationale 
> here exactly? It might be useful to always emit this reference, but also 
> append provider to the refs list in the other direction as well (that way the 
> results will be coherent again)
I think the rationale was that it provides more clarity in the VS Code UI. If 
you look back at the prototype image in the internal design doc, the references 
list always looks like a dropdown list with the containing file (in this case 
main file only) as title. In case of normal references, it shows the same 
symbol in different contexts, and it makes sense intuitively. In case of 
includes, it looks like a forest of unconnected symbols and might look like a 
bug to a user. IIRC that's the reason we initially agreed to add clarity by 
adding the include line itself.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155614/new/

https://reviews.llvm.org/D155614

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155614: [clangd] Remove redundant emptiness check in cross ref calculation for includes.

2023-07-19 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 541882.
VitaNuo marked 2 inline comments as done.
VitaNuo added a comment.

Remove only one of the checks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155614/new/

https://reviews.llvm.org/D155614

Files:
  clang-tools-extra/clangd/XRefs.cpp


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1367,9 +1367,6 @@
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1367,9 +1367,6 @@
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155614: [clangd] Make an include always refer to itself. Background: clang-review expects all referents to have definition, declaration or reference(s).

2023-07-18 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155614

Files:
  clang-tools-extra/clangd/XRefs.cpp


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1358,18 +1358,13 @@
 Result.Loc.uri = URIMainFile;
 Results.References.push_back(std::move(Result));
   });
-  if (Results.References.empty())
-return std::nullopt;
-
+ 
   // Add the #include line to the references list.
   ReferencesResult::Reference Result;
   Result.Loc.range =
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace


Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1358,18 +1358,13 @@
 Result.Loc.uri = URIMainFile;
 Results.References.push_back(std::move(Result));
   });
-  if (Results.References.empty())
-return std::nullopt;
-
+ 
   // Add the #include line to the references list.
   ReferencesResult::Reference Result;
   Result.Loc.range =
   rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
-
-  if (Results.References.empty())
-return std::nullopt;
   return Results;
 }
 } // namespace
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155215: [clangd] Fix the range for include reference to itself.

2023-07-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG96e50797d6ea: [clangd] Fix the range for include reference 
to itself. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155215/new/

https://reviews.llvm.org/D155215

Files:
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/clangd/SourceCode.cpp
  clang-tools-extra/clangd/SourceCode.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp

Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include   ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1362,10 +1362,9 @@
 return std::nullopt;
 
   // Add the #include line to the references list.
-  auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 1;
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range =
+  rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
 
Index: clang-tools-extra/clangd/SourceCode.h
===
--- clang-tools-extra/clangd/SourceCode.h
+++ clang-tools-extra/clangd/SourceCode.h
@@ -337,6 +337,10 @@
 /// using presumed locations. Returns \p Loc if it isn't inside preamble patch.
 SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
   const SourceManager );
+
+/// Returns the range starting at offset and spanning the whole line. Escaped
+/// newlines are not handled.
+clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset);
 } // namespace clangd
 } // namespace clang
 #endif
Index: clang-tools-extra/clangd/SourceCode.cpp
===
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -1242,5 +1242,17 @@
   }
   return Loc;
 }
+
+clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset) {
+  clangd::Range Result;
+  Result.end = Result.start = offsetToPosition(Code, HashOffset);
+
+  // Span the warning until the EOL or EOF.
+  Result.end.character +=
+  lspLength(Code.drop_front(HashOffset).take_until([](char C) {
+return C == '\n' || C == '\r';
+  }));
+  return Result;
+}
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -21,6 +21,7 @@
 #include "Diagnostics.h"
 #include "Headers.h"
 #include "ParsedAST.h"
+#include "Protocol.h"
 #include "clang-include-cleaner/Types.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Syntax/Tokens.h"
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -60,20 +60,6 @@
 
 namespace {
 
-// Returns the range starting at '#' and ending at EOL. Escaped newlines are not
-// handled.
-clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) {
-  clangd::Range Result;
-  Result.end = Result.start = offsetToPosition(Code, HashOffset);
-
-  // Span the warning until the EOL or EOF.
-  Result.end.character +=
-  lspLength(Code.drop_front(HashOffset).take_until([](char C) {
-return C == '\n' || C == '\r';
-  }));
-  return Result;
-}
-
 bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
   // Convert the path to Unix slashes and try to match against the filter.
   llvm::SmallString<64> NormalizedPath(HeaderPath);
@@ -224,7 +210,7 @@
 D.InsideMainFile = true;
 D.Severity = DiagnosticsEngine::Warning;
 D.Tags.push_back(Unnecessary);
-D.Range = getDiagnosticRange(Code, Inc->HashOffset);
+D.Range = rangeTillEOL(Code, Inc->HashOffset);
 // FIXME(kirillbobyrev): Removing inclusion might break the code if the
 // used headers are only reachable transitively through 

[PATCH] D155215: [clangd] Fix the range for include reference to itself.

2023-07-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked 2 inline comments as done.
VitaNuo added a comment.

Thanks for the comments!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155215/new/

https://reviews.llvm.org/D155215

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155215: [clangd] Fix the range for include reference to itself.

2023-07-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 540385.
VitaNuo added a comment.

Address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155215/new/

https://reviews.llvm.org/D155215

Files:
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/clangd/SourceCode.cpp
  clang-tools-extra/clangd/SourceCode.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp

Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include   ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1362,10 +1362,9 @@
 return std::nullopt;
 
   // Add the #include line to the references list.
-  auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 1;
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range =
+  rangeTillEOL(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
 
Index: clang-tools-extra/clangd/SourceCode.h
===
--- clang-tools-extra/clangd/SourceCode.h
+++ clang-tools-extra/clangd/SourceCode.h
@@ -337,6 +337,10 @@
 /// using presumed locations. Returns \p Loc if it isn't inside preamble patch.
 SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
   const SourceManager );
+
+/// Returns the range starting at offset and spanning the whole line. Escaped
+/// newlines are not handled.
+clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset);
 } // namespace clangd
 } // namespace clang
 #endif
Index: clang-tools-extra/clangd/SourceCode.cpp
===
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -1242,5 +1242,17 @@
   }
   return Loc;
 }
+
+clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset) {
+  clangd::Range Result;
+  Result.end = Result.start = offsetToPosition(Code, HashOffset);
+
+  // Span the warning until the EOL or EOF.
+  Result.end.character +=
+  lspLength(Code.drop_front(HashOffset).take_until([](char C) {
+return C == '\n' || C == '\r';
+  }));
+  return Result;
+}
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -21,6 +21,7 @@
 #include "Diagnostics.h"
 #include "Headers.h"
 #include "ParsedAST.h"
+#include "Protocol.h"
 #include "clang-include-cleaner/Types.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Syntax/Tokens.h"
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -60,20 +60,6 @@
 
 namespace {
 
-// Returns the range starting at '#' and ending at EOL. Escaped newlines are not
-// handled.
-clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) {
-  clangd::Range Result;
-  Result.end = Result.start = offsetToPosition(Code, HashOffset);
-
-  // Span the warning until the EOL or EOF.
-  Result.end.character +=
-  lspLength(Code.drop_front(HashOffset).take_until([](char C) {
-return C == '\n' || C == '\r';
-  }));
-  return Result;
-}
-
 bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
   // Convert the path to Unix slashes and try to match against the filter.
   llvm::SmallString<64> NormalizedPath(HeaderPath);
@@ -224,7 +210,7 @@
 D.InsideMainFile = true;
 D.Severity = DiagnosticsEngine::Warning;
 D.Tags.push_back(Unnecessary);
-D.Range = getDiagnosticRange(Code, Inc->HashOffset);
+D.Range = rangeTillEOL(Code, Inc->HashOffset);
 // FIXME(kirillbobyrev): Removing inclusion might break the code if the
 // used headers are only reachable transitively through this one. Suggest
 // including them directly instead.
___
cfe-commits mailing list

[PATCH] D155215: [clangd] Fix the range for include reference to itself.

2023-07-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 540321.
VitaNuo added a comment.

Re-use the logic from include cleaner in clangd.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155215/new/

https://reviews.llvm.org/D155215

Files:
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include   ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1362,10 +1362,9 @@
 return std::nullopt;
 
   // Add the #include line to the references list.
-  auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 
1;
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range =
+  getIncludeRange(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
 
Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -21,6 +21,7 @@
 #include "Diagnostics.h"
 #include "Headers.h"
 #include "ParsedAST.h"
+#include "Protocol.h"
 #include "clang-include-cleaner/Types.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Tooling/Syntax/Tokens.h"
@@ -82,6 +83,10 @@
 std::optional
 firstMatchedProvider(const include_cleaner::Includes ,
  llvm::ArrayRef Providers);
+
+// Returns the range starting at '#' and ending at EOL. Escaped newlines are 
not
+// handled.
+clangd::Range getIncludeRange(llvm::StringRef Code, unsigned HashOffset);
 } // namespace clangd
 } // namespace clang
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -58,11 +58,7 @@
 static bool AnalyzeStdlib = false;
 void setIncludeCleanerAnalyzesStdlib(bool B) { AnalyzeStdlib = B; }
 
-namespace {
-
-// Returns the range starting at '#' and ending at EOL. Escaped newlines are 
not
-// handled.
-clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) {
+clangd::Range getIncludeRange(llvm::StringRef Code, unsigned HashOffset) {
   clangd::Range Result;
   Result.end = Result.start = offsetToPosition(Code, HashOffset);
 
@@ -74,6 +70,8 @@
   return Result;
 }
 
+namespace {
+
 bool isIgnored(llvm::StringRef HeaderPath, HeaderFilter IgnoreHeaders) {
   // Convert the path to Unix slashes and try to match against the filter.
   llvm::SmallString<64> NormalizedPath(HeaderPath);
@@ -224,7 +222,7 @@
 D.InsideMainFile = true;
 D.Severity = DiagnosticsEngine::Warning;
 D.Tags.push_back(Unnecessary);
-D.Range = getDiagnosticRange(Code, Inc->HashOffset);
+D.Range = getIncludeRange(Code, Inc->HashOffset);
 // FIXME(kirillbobyrev): Removing inclusion might break the code if the
 // used headers are only reachable transitively through this one. Suggest
 // including them directly instead.


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include   ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1362,10 +1362,9 @@
 return std::nullopt;
 
   // Add the #include line to the references list.
-  auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 1;
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range =
+  

[PATCH] D155217: [clang-tidy][include-cleaner] Don't warn for the same symbol twice

2023-07-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Hi Alex,

Thank you for the patch.
We are aware that issuing one diagnostic per symbol reference might be 
overwhelming. However, we have decided to choose it over other options after 
considering the use cases that we'd like to cover.
This check is intended to be used in review tooling, where the user usually 
only sees their own change/diff. Issuing only one diagnostic for the first 
symbol reference would render this check mostly useless in code review, since 
the first symbol reference is not in the scope of the user's change most of the 
time.

Moreover, it's desirable to have similar behavior in include-cleaner checks 
across multiple clang tools. The current behaviour corresponds to the behaviour 
of built-in include-cleaner in clangd for similar reasons, i.e., users are most 
prone to act on a finding if it shows up in the piece of code they're currently 
editing.

The currently ongoing cleanup efforts should help the make the findings less 
overwhelming as well.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155217/new/

https://reviews.llvm.org/D155217

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155215: [clangd] Fix the range for include reference to itself.

2023-07-13 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155215

Files:
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1363,9 +1363,11 @@
 
   // Add the #include line to the references list.
   auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 
1;
+  auto Start =
+  offsetToPosition(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range = clangd::Range{
+  Start, Position{Inc.HashLine, Start.character + (int)IncludeLen}};
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
 


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -2299,7 +2299,7 @@
 
 TEST(FindReferences, UsedSymbolsFromInclude) {
   const char *Tests[] = {
-  R"cpp([[#include ^"bar.h"]]
+  R"cpp(   [[#include ^"bar.h"]]
 #include 
 int fstBar = [[bar1]]();
 int sndBar = [[bar2]]();
Index: clang-tools-extra/clangd/XRefs.cpp
===
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -1363,9 +1363,11 @@
 
   // Add the #include line to the references list.
   auto IncludeLen = std::string{"#include"}.length() + Inc.Written.length() + 1;
+  auto Start =
+  offsetToPosition(SM.getBufferData(SM.getMainFileID()), Inc.HashOffset);
   ReferencesResult::Reference Result;
-  Result.Loc.range = clangd::Range{Position{Inc.HashLine, 0},
-   Position{Inc.HashLine, (int)IncludeLen}};
+  Result.Loc.range = clangd::Range{
+  Start, Position{Inc.HashLine, Start.character + (int)IncludeLen}};
   Result.Loc.uri = URIMainFile;
   Results.References.push_back(std::move(Result));
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-07-13 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 539908.
VitaNuo marked 3 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/Preamble.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.h
  clang-tools-extra/clangd/index/FileIndex.cpp
  clang-tools-extra/clangd/index/FileIndex.h
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestWorkspace.cpp
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
===
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h
@@ -46,7 +46,7 @@
 
 /// We consider a macro to be a different symbol each time it is defined.
 struct Macro {
-  IdentifierInfo *Name;
+  const IdentifierInfo *Name;
   /// The location of the Name where the macro is defined.
   SourceLocation Definition;
 
Index: clang-tools-extra/clangd/unittests/TestWorkspace.cpp
===
--- clang-tools-extra/clangd/unittests/TestWorkspace.cpp
+++ clang-tools-extra/clangd/unittests/TestWorkspace.cpp
@@ -7,8 +7,10 @@
 //===--===//
 
 #include "TestWorkspace.h"
+#include "clang-include-cleaner/Record.h"
 #include "index/FileIndex.h"
 #include "gtest/gtest.h"
+#include 
 #include 
 
 namespace clang {
@@ -21,14 +23,12 @@
   continue;
 TU.Code = Input.second.Code;
 TU.Filename = Input.first().str();
-TU.preamble(
-[&](CapturedASTCtx ASTCtx,
-const std::shared_ptr CanonIncludes) {
-  auto  = ASTCtx.getASTContext();
-  auto  = ASTCtx.getPreprocessor();
-  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
-*CanonIncludes);
-});
+TU.preamble([&](CapturedASTCtx ASTCtx,
+std::shared_ptr PI) {
+  auto  = ASTCtx.getASTContext();
+  auto  = ASTCtx.getPreprocessor();
+  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, *PI);
+});
 ParsedAST MainAST = TU.build();
 Index->updateMain(testPath(Input.first()), MainAST);
   }
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -162,9 +162,9 @@
 
 SymbolSlab TestTU::headerSymbols() const {
   auto AST = build();
-  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
-AST.getPreprocessor(),
-AST.getCanonicalIncludes()));
+  return std::get<0>(indexHeaderSymbols(
+  /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
+  *AST.getPragmaIncludes()));
 }
 
 RefSlab TestTU::headerRefs() const {
@@ -177,7 +177,7 @@
   auto Idx = std::make_unique();
   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
   AST.getASTContext(), AST.getPreprocessor(),
-  AST.getCanonicalIncludes());
+  *AST.getPragmaIncludes());
   Idx->updateMain(testPath(Filename), AST);
   return std::move(Idx);
 }
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -18,6 +18,7 @@
 #include "TUScheduler.h"
 #include "TestFS.h"
 #include "TestIndex.h"
+#include "clang-include-cleaner/Record.h"
 #include "support/Cancellation.h"
 #include "support/Context.h"
 #include "support/Path.h"
@@ -1134,9 +1135,9 @@
   public:
 

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-07-13 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked 19 inline comments as done.
VitaNuo added a comment.

Thank you!




Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:839
+  if (!Headers.empty())
+SymbolProviders.insert({S.ID, Headers[0]});
 }

kadircet wrote:
> once we have the optional as the value type you can also rewrite this 
> as:
> ```
> auto [It, Inserted] = SymbolProviders.try_emplace(S.ID);
> if (Inserted) {
>   auto Providers = include_cleaner::headersForSymbol(Sym, SM, 
> Opts.PragmaIncludes.get());
>   if(!Providers.empty()) It->second = Providers.front();
> } 
> ```
> 
> that way we can get rid of some redundant calls to `headersForSymbol`
Sure, although I'm not sure where the redundant calls would be coming from. 
I've been under the impression that this function is supposed to be called once 
for each symbol.



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:913
+if (!Inserted)
+  continue;
+switch (H.kind()) {

kadircet wrote:
> we shouldn't `continue` here, it just means we can directly use `SpellingIt` 
> to figure out what to put into `NewSym.IncludeHeaders`.
> 
> maybe something like:
> 
> ```
> auto [SpellingIt, Inserted] = HeaderSpelling.try_emplace(H);
> if (Inserted)
>   SpellingIt-> second = getSpelling(H, *PP);
> if(!SpellingIt->second.empty()) {
>   Symbol NewSym = *S;
>   NewSym.IncludeHeaders.push_back({SpellingIt->second, 1, Directives});
>   Symbols.insert(NewSym);
> }
> ```
> 
> ```
> std::string getSpelling(const include_cleaner::Header , const Preprocessor 
> ) {
>   if(H.kind() == Physical) {
>// Check if we have a mapping for the header in system includes.
>// FIXME: get rid of this once stdlib mapping covers these system headers 
> too.
>CanonicalIncludes CI;
>CI.addSystemHeadersMapping(PP.getLangOpts());
> if (auto Canonical = CI.mapHeader(H.physical()->getLastRef()); 
> !Canonical.empty())
> return Canonical;
> if (!tooling::isSelfContainedHeader(...))
> return "";
>   }
>   // Otherwise use default spelling strategies in include_cleaner.
>   return include_cleaner::spellHeader(H);
> }
> ```
As agreed offline, it is not easy to split out a spelling function, since 
`HeaderFileURIs` is a private member of the symbol collector, and we seem to 
still need to generate URIs for physical headers going forward.  



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:877
   // We delay this until end of TU so header guards are all resolved.
   for (const auto &[SID, FID] : IncludeFiles) {
 if (const Symbol *S = Symbols.find(SID)) {

kadircet wrote:
> VitaNuo wrote:
> > kadircet wrote:
> > > let's iterate over `SymbolProviders` instead, as `IncludeFiles` is a 
> > > legacy struct now.
> > Not sure I'm following. Iterating over `SymbolProviders` means retrieving 
> > `include_cleaner::Header`s. How would I handle the entire Obj-C part then, 
> > without the FID of the include header? 
> we're joining `IncludeFiles` and `SymbolProviders`, as they have the same 
> keys.
> 
> right now you're iterating over the keys in `IncludeFiles` and doing a lookup 
> into `SymbolProviders` using that key to get providers.
> we can also do the iteration over `SymbolProviders` and do the lookup into 
> `IncludeFiles` instead to find associated `FID`.
Ok, makes sense now, thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154962: [clangd] Use canonical path as resolved path for includes.

2023-07-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG7322f2d5ed54: [clangd] Use canonical path as resolved path 
for includes. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154962/new/

https://reviews.llvm.org/D154962

Files:
  clang-tools-extra/clangd/Headers.cpp


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,9 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  Inc.Resolved = std::string(
+  File ? getCanonicalPath(*File, SM.getFileManager()).value_or("")
+   : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,9 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  Inc.Resolved = std::string(
+  File ? getCanonicalPath(*File, SM.getFileManager()).value_or("")
+   : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154962: [clangd] Use canonical path as resolved path for includes.

2023-07-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo marked an inline comment as done.
VitaNuo added a comment.

Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154962/new/

https://reviews.llvm.org/D154962

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154962: [clangd] Use canonical path as resolved path for includes.

2023-07-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 539121.
VitaNuo added a comment.

Address comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154962/new/

https://reviews.llvm.org/D154962

Files:
  clang-tools-extra/clangd/Headers.cpp


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,9 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  Inc.Resolved = std::string(
+  File ? getCanonicalPath(*File, SM.getFileManager()).value_or("")
+   : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,9 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  Inc.Resolved = std::string(
+  File ? getCanonicalPath(*File, SM.getFileManager()).value_or("")
+   : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154962: [clangd] Use canonical path as resolved path for includes.

2023-07-11 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154962

Files:
  clang-tools-extra/clangd/Headers.cpp


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,11 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  auto CanonicalPath =
+  File ? getCanonicalPath(File->getFileEntry().getLastRef(),
+  SM.getFileManager())
+   : std::nullopt;
+  Inc.Resolved = std::string(CanonicalPath ? *CanonicalPath : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;


Index: clang-tools-extra/clangd/Headers.cpp
===
--- clang-tools-extra/clangd/Headers.cpp
+++ clang-tools-extra/clangd/Headers.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Support/Path.h"
 #include 
 #include 
+#include 
 
 namespace clang {
 namespace clangd {
@@ -53,8 +54,11 @@
   auto  = Out->MainFileIncludes.back();
   Inc.Written =
   (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
-  Inc.Resolved =
-  std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
+  auto CanonicalPath =
+  File ? getCanonicalPath(File->getFileEntry().getLastRef(),
+  SM.getFileManager())
+   : std::nullopt;
+  Inc.Resolved = std::string(CanonicalPath ? *CanonicalPath : "");
   Inc.HashOffset = SM.getFileOffset(HashLoc);
   Inc.HashLine =
   SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154068: [clangd] Don't show header for namespace decl in Hover

2023-06-29 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154068/new/

https://reviews.llvm.org/D154068

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-06-29 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for comments!




Comment at: clang-tools-extra/clangd/index/CanonicalIncludes.h:40
   /// Adds a file-to-string mapping from \p ID to \p CanonicalPath.
   void addMapping(FileEntryRef Header, llvm::StringRef CanonicalPath);
 

kadircet wrote:
> AFAICT, only users of this endpoint were in `IWYUCommentHandler` (and some 
> tests). we can also get rid of this one now.
> 
> More importantly now this turns into a mapper used only by symbolcollector, 
> that can be cheaply created at use time (we just copy around a pointer every 
> time we want to create it). so you can drop `CanonicalIncludes` from all of 
> the interfaces, including `SymbolCollector::Options` and create one on demand 
> in `HeaderFileURICache`. all we need is langopts, and it's available through 
> preprocessor (not necessarily on construction, but at the time when we want 
> to do the mappings).
> 
> As you're already touching all of the interfaces that propagate 
> `CanonicalIncludes` around, hopefully this change should only make things 
> simpler (and you already need to touch them when you rebase), but if that 
> feels like too much churn in this patch feel free to do that in a follow up 
> as well.
> ... and create one on demand in HeaderFileURICache. all we need is langopts, 
> and it's available through preprocessor (not necessarily on construction, but 
> at the time when we want to do the mappings

This time sounds a little late, since we don't want to rebuild the system 
header mapping every time we request a mapping for a particular header. 
Cache construction time doesn't work, since it happens before the preprocessor 
is set in the symbol collector (and we need the preprocessor to retrieve 
language options).
So far the safe place I've found is right after the preprocessor is set in the 
symbol collector. Another option is to collect the system header options in the 
finish() method, but I can't see why we would need to delay until then. 



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:250
+  // of whether it's an otherwise-good header (header guards etc).
+  llvm::StringRef mapCanonical(FileID FID) {
+if (SysHeaderMapping) {

kadircet wrote:
> let's change the interface here to take in a `FileEntry` instead.
I believe it has to be `FileEntryRef` instead. This is what the 
`CanonicalIncludes::mapHeader` expects, and this is also seems right (somewhere 
in the Clang docs I've read "everything that needs a name should use 
`FileEntryRef`", and name is exactly what `CanonicalIncludes::mapHeader` 
needs). 

For the case of a physical header (i.e., `FileEntry` instance) I've switched to 
using `getLastRef` now. There's also another API `FileManager::getFileRef` 
where I could try passing the result of `tryGetRealPathName`. I am not sure I 
have enough information to judge if any of these options is distinctly better.



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:412-413
+
+if (auto Verbatim = PI->getPublic(FileEntry); !Verbatim.empty())
+  return Verbatim;
+

kadircet wrote:
> let's move this logic into `mapCanonical` too.
This would deliver wrong results for C++. We would basically get every 
IWYU-public header twice: once through the `verbatim` branch of the 
include_cleaner results, and then once again when mapping the physical private 
file to a canonical version (e.g., for system headers).



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:849
+  shouldCollectIncludePath(S.SymInfo.Kind) != Symbol::Invalid)
+SymbolProviders[S.ID] = std::move(Headers);
+}

kadircet wrote:
> because you're taking in `Headers` as `const`, this move is not actually 
> moving.
Ok, this is not relevant anymore, but I am not sure I understand why. Am I not 
allowed to say that I don't need this variable/memory anymore, even though it's 
`const`?



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:877
   // We delay this until end of TU so header guards are all resolved.
   for (const auto &[SID, FID] : IncludeFiles) {
 if (const Symbol *S = Symbols.find(SID)) {

kadircet wrote:
> let's iterate over `SymbolProviders` instead, as `IncludeFiles` is a legacy 
> struct now.
Not sure I'm following. Iterating over `SymbolProviders` means retrieving 
`include_cleaner::Header`s. How would I handle the entire Obj-C part then, 
without the FID of the include header? 



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:930
+auto Canonical =
+HeaderFileURIs->mapCanonical(SM.getOrCreateFileID(
+H.physical(), SrcMgr::CharacteristicKind::C_User));

kadircet wrote:
> no need for the `getOrCreateFileID` after you change parameter for 
> `mapCanonical` to be a file entry.
Still need the 

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-06-29 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 535740.
VitaNuo marked 27 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/ParsedAST.h
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/Preamble.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/XRefs.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.h
  clang-tools-extra/clangd/index/FileIndex.cpp
  clang-tools-extra/clangd/index/FileIndex.h
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestWorkspace.cpp

Index: clang-tools-extra/clangd/unittests/TestWorkspace.cpp
===
--- clang-tools-extra/clangd/unittests/TestWorkspace.cpp
+++ clang-tools-extra/clangd/unittests/TestWorkspace.cpp
@@ -7,8 +7,10 @@
 //===--===//
 
 #include "TestWorkspace.h"
+#include "clang-include-cleaner/Record.h"
 #include "index/FileIndex.h"
 #include "gtest/gtest.h"
+#include 
 #include 
 
 namespace clang {
@@ -21,14 +23,12 @@
   continue;
 TU.Code = Input.second.Code;
 TU.Filename = Input.first().str();
-TU.preamble(
-[&](CapturedASTCtx ASTCtx,
-const std::shared_ptr CanonIncludes) {
-  auto  = ASTCtx.getASTContext();
-  auto  = ASTCtx.getPreprocessor();
-  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
-*CanonIncludes);
-});
+TU.preamble([&](CapturedASTCtx ASTCtx,
+std::shared_ptr PI) {
+  auto  = ASTCtx.getASTContext();
+  auto  = ASTCtx.getPreprocessor();
+  Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP, PI);
+});
 ParsedAST MainAST = TU.build();
 Index->updateMain(testPath(Input.first()), MainAST);
   }
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -164,9 +164,9 @@
 
 SymbolSlab TestTU::headerSymbols() const {
   auto AST = build();
-  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
-AST.getPreprocessor(),
-AST.getCanonicalIncludes()));
+  return std::get<0>(indexHeaderSymbols(
+  /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
+  AST.getPragmaIncludes()));
 }
 
 RefSlab TestTU::headerRefs() const {
@@ -179,7 +179,7 @@
   auto Idx = std::make_unique();
   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
   AST.getASTContext(), AST.getPreprocessor(),
-  AST.getCanonicalIncludes());
+  AST.getPragmaIncludes());
   Idx->updateMain(testPath(Filename), AST);
   return std::move(Idx);
 }
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -18,6 +18,7 @@
 #include "TUScheduler.h"
 #include "TestFS.h"
 #include "TestIndex.h"
+#include "clang-include-cleaner/Record.h"
 #include "support/Cancellation.h"
 #include "support/Context.h"
 #include "support/Path.h"
@@ -1129,9 +1130,9 @@
   public:
 BlockPreambleThread(llvm::StringRef BlockVersion, Notification )
 : BlockVersion(BlockVersion), N(N) {}
-void
-onPreambleAST(PathRef Path, llvm::StringRef Version, CapturedASTCtx,
-  const std::shared_ptr) override {
+void onPreambleAST(
+PathRef Path, llvm::StringRef Version, CapturedASTCtx,
+std::shared_ptr) override {
   if (Version == BlockVersion)
 N.wait();
 }
@@ -1208,9 +1209,9 @@
 BlockPreambleThread(Notification , DiagsCB CB)
 : UnblockPreamble(UnblockPreamble), CB(std::move(CB)) {}
 
-void
-onPreambleAST(PathRef Path, llvm::StringRef Version, CapturedASTCtx,
-

[PATCH] D153769: [clangd] Implement the 'Organize Imports' source action. Fix include-cleaner findings in batch.

2023-06-26 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153769

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
  clang-tools-extra/clangd/refactor/tweaks/OrganizeImports.cpp
  clang-tools-extra/clangd/test/code-action-request.test
  clang-tools-extra/clangd/test/fixits-codeaction-documentchanges.test
  clang-tools-extra/clangd/test/fixits-codeaction.test
  clang-tools-extra/clangd/test/fixits-command-documentchanges.test
  clang-tools-extra/clangd/test/fixits-command.test
  clang-tools-extra/clangd/test/include-cleaner-batch-fix.test

Index: clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
===
--- clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
+++ clang-tools-extra/clangd/test/include-cleaner-batch-fix.test
@@ -300,6 +300,26 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  "command": "clangd.applyFix",
 # CHECK-NEXT:  "title": "Apply fix: fix all includes"
+# CHECK-NEXT:},
+# CHECK-NEXT:{
+# CHECK-NEXT:  "arguments": [
+# CHECK-NEXT:{
+# CHECK-NEXT:  "file": "file:///clangd-test/simple.cpp",
+# CHECK-NEXT:"selection": {
+# CHECK-NEXT:"end": {
+# CHECK-NEXT:  "character": 4,
+# CHECK-NEXT:  "line": 2
+# CHECK-NEXT:},
+# CHECK-NEXT:"start": {
+# CHECK-NEXT:  "character": 1,
+# CHECK-NEXT:  "line": 2
+# CHECK-NEXT:}
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "tweakID": "OrganizeImports"
+# CHECK-NEXT:}
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  "command": "clangd.applyTweak",
+# CHECK-NEXT:  "title": "Remove unused and add missing includes"
 # CHECK-NEXT:}
 # CHECK-NEXT:  ]
 ---
@@ -485,6 +505,26 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  "command": "clangd.applyFix",
 # CHECK-NEXT:  "title": "Apply fix: fix all includes"
+# CHECK-NEXT:},
+# CHECK-NEXT: {
+# CHECK-NEXT:  "arguments": [
+# CHECK-NEXT:{
+# CHECK-NEXT:  "file": "file:///clangd-test/simple.cpp",
+# CHECK-NEXT:  "selection": {
+# CHECK-NEXT:"end": {
+# CHECK-NEXT:  "character": 17,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT:},
+# CHECK-NEXT:"start": {
+# CHECK-NEXT:  "character": 0,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT:}
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "tweakID": "OrganizeImports"
+# CHECK-NEXT:}
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  "command": "clangd.applyTweak",
+# CHECK-NEXT:  "title": "Remove unused and add missing includes"
 # CHECK-NEXT:}
 # CHECK-NEXT:  ]
 ---
Index: clang-tools-extra/clangd/test/fixits-command.test
===
--- clang-tools-extra/clangd/test/fixits-command.test
+++ clang-tools-extra/clangd/test/fixits-command.test
@@ -92,6 +92,26 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  "command": "clangd.applyFix",
 # CHECK-NEXT:  "title": "Apply fix: use '==' to turn this assignment into an equality comparison"
+# CHECK-NEXT:},
+# CHECK-NEXT:{
+# CHECK-NEXT:  "arguments": [
+# CHECK-NEXT:{
+# CHECK-NEXT:  "file": "file:///clangd-test/foo.c",
+# CHECK-NEXT:  "selection": {
+# CHECK-NEXT:"end": {
+# CHECK-NEXT:  "character": 35,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT:},
+# CHECK-NEXT:"start": {
+# CHECK-NEXT:  "character": 13,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT:}
+# CHECK-NEXT:  },
+# CHECK-NEXT:  "tweakID": "OrganizeImports"
+# CHECK-NEXT:}
+# CHECK-NEXT:  ],
+# CHECK-NEXT:  "command": "clangd.applyTweak",
+# CHECK-NEXT:  "title": "Remove unused and add missing includes"
 # CHECK-NEXT:}
 # CHECK-NEXT:  ]
 ---
@@ -162,6 +182,26 @@
 # CHECK-NEXT:  ],
 # CHECK-NEXT:  "command": "clangd.applyFix",
 # CHECK-NEXT:  "title": "Apply fix: use '==' to turn this assignment into an equality comparison"
+# CHECK-NEXT:},
+# CHECK-NEXT:{
+# CHECK-NEXT:  "arguments": [
+# CHECK-NEXT:{
+# CHECK-NEXT:  "file": "file:///clangd-test/foo.c",
+# CHECK-NEXT:  "selection": {
+# CHECK-NEXT:"end": {
+# CHECK-NEXT:  "character": 35,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT:},
+# CHECK-NEXT:"start": {
+# CHECK-NEXT:  "character": 13,
+# CHECK-NEXT:  "line": 0
+# CHECK-NEXT: 

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-06-20 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the comments!




Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:393-395
+const auto *FileEntry = SM.getFileEntryForID(FID);
+for (const auto *Export : PI.getExporters(FileEntry, SM.getFileManager()))
+  return toURI(Export->tryGetRealPathName());

kadircet wrote:
> sorry i don't understand what this logic is trying to achieve.
> 
> we seem to be prefering "first" exporting header, over the headers that 
> directly provide the symbol. Also comment says it's done for objc, but 
> there's nothing limiting this logic to ObjC. any chance this is unintended?
No, this was intended, but possibly I didn't get it right.

>  comment says it's done for objc, but there's nothing limiting this logic to 
> objc

AFAIU this whole code path will now only be reachable for objc symbols.  I have 
removed pragma includes from the canonical include mapping now, leaving the 
pragma handling to include cleaner. However, that only triggers for C/C++, so 
in case we need the `export` and `private` pragmas for objc, we need to 
re-insert the handling for them somewhere. 
There is no pre-existing test case for pragmas in objc and there is no usage of 
these pragmas for objc code in the code base either,  so I have no way of 
knowing if this is actually needed. But I believe you've mentioned in some 
discussion we should still handle the pragmas for objc.

> we seem to be preferring "first" exporting header, over the headers that 
> directly provide the symbol.

Isn't that correct if there's an `IWYU export` pragma involved? The snippet 
comes from `include_cleaner::findHeaders`, with the only difference that it 
stops at the first exporter (since the canonical include mapping also just 
stored one mapping for the header).

Let me know how to do it better, or maybe if this is necessary at all. 
Honestly, I am not sure about this, since, as mentioned, there are no `export` 
or `private/public` pragmas in objc files in the codebase atm.




Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:903
+NewSym = *S;
+if (!IncludeHeader.empty()) {
   NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});

kadircet wrote:
> this is using legacy mappings for non-objc symbols too
Removed this whole passage.



Comment at: clang-tools-extra/clangd/index/SymbolCollector.cpp:934-936
+  SymbolIncludeSpelling[SID] = HeaderFileURIs->getIncludeHeader(
+  ASTCtx->getSourceManager().getOrCreateFileID(
+  H.physical(), SrcMgr::CharacteristicKind::C_User));

kadircet wrote:
> we actually only want to use `HeaderFileURIs->toURI` here and not the 
> `getIncludeHeader`, because:
> - We want to make use of mapping logic in include-cleaner solely, and we 
> already have all that information in Providers mapping.
> - The extra logic we want to apply for ObjC symbols are already applied 
> before reaching here.
> 
> the way we create a FileID here is a little bit iffy, by using `toURI`, 
> hopefully we can avoid that conversion.
> 
> the only remaining issue is, we actually still want to use system header 
> mappings inside `CanonicalIncludes` until we have better support for them in 
> include-cleaner library itself. So I think we should still call 
> `Includes->mapHeader(H.physical())` before using `toURI`.
Ok SGTM. It seems, though, that we still need the "iffy" FID for the canonical 
mapping.. Let me know if you know a better way. 





Comment at: clang-tools-extra/clangd/index/SymbolCollector.h:131
+  }
+  void setPreprocessor(Preprocessor ) {
+this->PP = 

kadircet wrote:
> unfortunately this alternative is called after we've parsed the file, from 
> `clangd/index/FileIndex.cpp`, `indexSymbols`. hence attaching `PI` to 
> `PPCallbacks` won't have any effect.
> 
> This is working unintentionally because we're still populating 
> `CanonicalIncludes` with legacy IWYUPragmaHandler in clangd, and using that 
> to perform private -> public mappings.
> 
> We should instead propagate the `PragmaIncludes` we have in `ParsedAST` and 
> in preamble builds here. You should be able to test the new behaviour 
> `clang-tools-extra/clangd/unittests/FileIndexTests.cpp` to make sure pragma 
> handling through dynamic indexing code paths keep working as expected using 
> an `export` pragma.
Ok thank you. I'm using `PragmaIncludes` from AST and preamble builds for the 
dynamic index now.

For the background index, I've finally managed to move pragma recording to the 
`IndexAction` now.

I've also removed the redundant (to include cleaner) comment handlers from the 
AST build and preamble building logic. 

Also removed the `mapSymbol` method from `CanonicalIncludes`, since it had one 
usage which should now be covered by the include cleaner, I believe. 


Repository:
  rG LLVM Github Monorepo

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-06-20 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 532964.
VitaNuo marked 8 inline comments as done.
VitaNuo added a comment.
Herald added a subscriber: javed.absar.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152900/new/

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ParsedAST.cpp
  clang-tools-extra/clangd/Preamble.cpp
  clang-tools-extra/clangd/Preamble.h
  clang-tools-extra/clangd/TUScheduler.cpp
  clang-tools-extra/clangd/TUScheduler.h
  clang-tools-extra/clangd/index/CanonicalIncludes.cpp
  clang-tools-extra/clangd/index/CanonicalIncludes.h
  clang-tools-extra/clangd/index/FileIndex.cpp
  clang-tools-extra/clangd/index/FileIndex.h
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/tool/Check.cpp
  clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
  clang-tools-extra/clangd/unittests/FileIndexTests.cpp
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
  clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestWorkspace.cpp

Index: clang-tools-extra/clangd/unittests/TestWorkspace.cpp
===
--- clang-tools-extra/clangd/unittests/TestWorkspace.cpp
+++ clang-tools-extra/clangd/unittests/TestWorkspace.cpp
@@ -7,6 +7,7 @@
 //===--===//
 
 #include "TestWorkspace.h"
+#include "clang-include-cleaner/Record.h"
 #include "index/FileIndex.h"
 #include "gtest/gtest.h"
 #include 
@@ -22,9 +23,10 @@
 TU.Code = Input.second.Code;
 TU.Filename = Input.first().str();
 TU.preamble([&](ASTContext , Preprocessor ,
-const CanonicalIncludes ) {
+const CanonicalIncludes ,
+const include_cleaner::PragmaIncludes *PI) {
   Index->updatePreamble(testPath(Input.first()), "null", Ctx, PP,
-CanonIncludes);
+CanonIncludes, PI);
 });
 ParsedAST MainAST = TU.build();
 Index->updateMain(testPath(Input.first()), MainAST);
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -164,9 +164,9 @@
 
 SymbolSlab TestTU::headerSymbols() const {
   auto AST = build();
-  return std::get<0>(indexHeaderSymbols(/*Version=*/"null", AST.getASTContext(),
-AST.getPreprocessor(),
-AST.getCanonicalIncludes()));
+  return std::get<0>(indexHeaderSymbols(
+  /*Version=*/"null", AST.getASTContext(), AST.getPreprocessor(),
+  AST.getCanonicalIncludes(), AST.getPragmaIncludes()));
 }
 
 RefSlab TestTU::headerRefs() const {
@@ -179,7 +179,7 @@
   auto Idx = std::make_unique();
   Idx->updatePreamble(testPath(Filename), /*Version=*/"null",
   AST.getASTContext(), AST.getPreprocessor(),
-  AST.getCanonicalIncludes());
+  AST.getCanonicalIncludes(), AST.getPragmaIncludes());
   Idx->updateMain(testPath(Filename), AST);
   return std::move(Idx);
 }
Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
===
--- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -18,6 +18,7 @@
 #include "TUScheduler.h"
 #include "TestFS.h"
 #include "TestIndex.h"
+#include "clang-include-cleaner/Record.h"
 #include "support/Cancellation.h"
 #include "support/Context.h"
 #include "support/Path.h"
@@ -1131,7 +1132,8 @@
 : BlockVersion(BlockVersion), N(N) {}
 void onPreambleAST(PathRef Path, llvm::StringRef Version,
const CompilerInvocation &, ASTContext ,
-   Preprocessor &, const CanonicalIncludes &) override {
+   Preprocessor &, const CanonicalIncludes &,
+   const include_cleaner::PragmaIncludes *) override {
   if (Version == BlockVersion)
 N.wait();
 }
@@ -1210,7 +1212,8 @@
 
 void onPreambleAST(PathRef Path, llvm::StringRef Version,
const CompilerInvocation &, ASTContext ,
-   Preprocessor &, const CanonicalIncludes &) override {
+   Preprocessor &, const CanonicalIncludes &,
+   const include_cleaner::PragmaIncludes *) override {
   if (BuildBefore)
 ASSERT_TRUE(UnblockPreamble.wait(timeoutSeconds(5)))
 << "Expected notification";
@@ -1564,7 +1567,8 @@
 : 

[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks!




Comment at: clang-tools-extra/clangd/unittests/HoverTests.cpp:3726
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {

hokein wrote:
> nit: instead of creating a completely-new TEST, it seems simpler to just add 
> a testcase in the existing `TEST(Hover, All)`.
> 
> 
> ```
>{R"cpp(// Should not crash on an invalid param decl.
>   class Foo {};
>   // error-ok
>   void foo(Foo [[fo^o]] = nullptr);
>   )cpp",
>[](HoverInfo ) {
>  HI.Name = "foo";
>  HI.Type = "Foo";
>  HI.Kind = index::SymbolKind::Parameter;
>  HI.NamespaceScope = "";
>  HI.LocalScope = "foo::";
>  HI.Definition = "Foo foo = ";
>}},
> ```
I'm not sure I agree. The `Hover, All` test takes already about 5 seconds to 
scroll through on the screen :)

On top of that, other test cases that are a result of crash fixes seem to be 
separate too.
Therefore, I'd prefer to keep it as a separate test case for now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
VitaNuo marked an inline comment as done.
Closed by commit rGc9888dce4474: [clangd] Skip function parameter decls when 
evaluating variables on hover. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr);
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr);
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr);
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr);
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added inline comments.



Comment at: clang-tools-extra/clangd/Hover.cpp:663
+  const auto *Var = dyn_cast(D);
+  if (Var && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())

VitaNuo wrote:
> hokein wrote:
> > We're ignoring all `ParmVarDecl` cases, 
> > 
> > The crash here is for broken code. For the crash case, the AST node looks 
> > like 
> > 
> > ```
> > `-ParmVarDecl 0x563b7cc853c0  col:14 invalid param 
> > 'Foo':'Foo' cinit
> > `-OpaqueValueExpr 0x563b7cc854a0  'Foo':'Foo'
> > ```
> > 
> > One alternative is to exclude invalid `VarDecl`, then the Hover feature 
> > still keep working on valid `ParmVarDecl`.
> > 
> > ```
> > if (const auto* Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
> >...
> > }
> > ```
> Why would you do that?
Sorry this was an unintended comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the review!




Comment at: clang-tools-extra/clangd/Hover.cpp:663
+  const auto *Var = dyn_cast(D);
+  if (Var && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())

hokein wrote:
> We're ignoring all `ParmVarDecl` cases, 
> 
> The crash here is for broken code. For the crash case, the AST node looks 
> like 
> 
> ```
> `-ParmVarDecl 0x563b7cc853c0  col:14 invalid param 
> 'Foo':'Foo' cinit
> `-OpaqueValueExpr 0x563b7cc854a0  'Foo':'Foo'
> ```
> 
> One alternative is to exclude invalid `VarDecl`, then the Hover feature still 
> keep working on valid `ParmVarDecl`.
> 
> ```
> if (const auto* Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
>...
> }
> ```
Why would you do that?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 532044.
VitaNuo marked 2 inline comments as done.
VitaNuo added a comment.

Simplify.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr);
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr);
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr);
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr);
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 532042.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr) {}
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr) {}
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -22,6 +22,7 @@
 
 #include "gtest/gtest.h"
 #include 
+#include 
 #include 
 #include 
 
@@ -3722,6 +3723,34 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluatedOnInvalidDecls) {
+  struct {
+const char *const Code;
+const std::optional HoverValue;
+  } Cases[] = {
+  {R"cpp(
+// error-ok testing behavior on invalid decl
+class Foo {};
+void foo(Foo p^aram = nullptr) {}
+)cpp",
+   std::nullopt},
+  {R"cpp(
+class Foo {};
+void foo(Foo *p^aram = nullptr) {}
+)cpp",
+   "nullptr"},
+  };
+
+  for (const auto  : Cases) {
+Annotations T(C.Code);
+TestTU TU = TestTU::withCode(T.code());
+auto AST = TU.build();
+auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ASSERT_TRUE(HI);
+ASSERT_EQ(HI->Value, C.HoverValue);
+  }
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,7 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  if (const auto *Var = dyn_cast(D); Var && !Var->isInvalidDecl()) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153018: [include-cleaner] Reorder SymbolReference fields to avoid padding space, NFC

2023-06-16 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153018/new/

https://reviews.llvm.org/D153018

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-15 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

thanks!




Comment at: clang-tools-extra/clangd/unittests/HoverTests.cpp:3726
+TEST(Hover, FunctionParameterDefaulValueNotEvaluated) {
+  Annotations T("void foo(int p^aram = 5);");
+  TestTU TU = TestTU::withCode(T.code());

hokein wrote:
> I believe this case should trigger a crash, but I don't see the crash with a 
> trunk-built clangd, do we miss something here?
It crashes on invalid code. I've inspected two user workspaces:
1.

```
class Foo{};
void foo(Foo param = nullptr);
```

2.
```
void foo(ClassName param = 
functionReturningObjectOfSimilarSoundingButUnrelatedClass());
```

I guess `clangd` is not even expected to act soundly in the face of 
non-compiling code. 
But since these crashes are easy to get rid of, and evaluating parameters in 
function declarations is pointless anyways, we can just avoid these crashes at 
no extra cost. I cannot use non-compiling code in a hover test, though.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152542: [clangd] Use include_cleaner spelling strategies in clangd.

2023-06-15 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Duplicate of https://reviews.llvm.org/D152913. Landed the other one.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152542/new/

https://reviews.llvm.org/D152542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153013: [clang-tidy] Correct the include-cleaner-check diagnostic message for missing-includes.

2023-06-15 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo accepted this revision.
VitaNuo added a comment.
This revision is now accepted and ready to land.

Thanks!




Comment at: clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp:49
   SourceLocation SymRefLocation;
+  include_cleaner::Symbol Sym;
   include_cleaner::Header Missing;

Consider making the whole `include_cleaner::SymbolReference` a field instead, 
since you're already using two out of its three fields here. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153013/new/

https://reviews.llvm.org/D153013

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-15 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 531718.
VitaNuo added a comment.

Simplify.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153015/new/

https://reviews.llvm.org/D153015

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3722,6 +3722,15 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluated) {
+  Annotations T("void foo(int p^aram = 5);");
+  TestTU TU = TestTU::withCode(T.code());
+  auto AST = TU.build();
+  auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(HI);
+  ASSERT_FALSE(HI->Value);
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,8 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  const auto *Var = dyn_cast(D);
+  if (Var && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3722,6 +3722,15 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluated) {
+  Annotations T("void foo(int p^aram = 5);");
+  TestTU TU = TestTU::withCode(T.code());
+  auto AST = TU.build();
+  auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(HI);
+  ASSERT_FALSE(HI->Value);
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,8 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  const auto *Var = dyn_cast(D);
+  if (Var && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153015: [clangd] Skip function parameter decls when evaluating variables on hover.

2023-06-15 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153015

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3722,6 +3722,18 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluated) {
+  Annotations T(R"cpp(
+  void foo(int p^aram = 5);
+  )cpp");
+
+  TestTU TU = TestTU::withCode(T.code());
+  auto AST = TU.build();
+  auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(HI);
+  ASSERT_FALSE(HI->Value);
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,8 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  const auto *Var = dyn_cast(D);
+  if (Var != nullptr && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3722,6 +3722,18 @@
   EXPECT_EQ(*HI->Value, "");
 }
 
+TEST(Hover, FunctionParameterDefaulValueNotEvaluated) {
+  Annotations T(R"cpp(
+  void foo(int p^aram = 5);
+  )cpp");
+
+  TestTU TU = TestTU::withCode(T.code());
+  auto AST = TU.build();
+  auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+  ASSERT_TRUE(HI);
+  ASSERT_FALSE(HI->Value);
+}
+
 TEST(Hover, DisableShowAKA) {
   Annotations T(R"cpp(
 using m_int = int;
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -659,7 +659,8 @@
 HI.Type = printType(TAT->getTemplatedDecl()->getUnderlyingType(), Ctx, PP);
 
   // Fill in value with evaluated initializer if possible.
-  if (const auto *Var = dyn_cast(D)) {
+  const auto *Var = dyn_cast(D);
+  if (Var != nullptr && !llvm::isa(Var)) {
 if (const Expr *Init = Var->getInit())
   HI.Value = printExprValue(Init, Ctx);
   } else if (const auto *ECD = dyn_cast(D)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152913: [clangd] Use include_cleaner spelling strategies in clangd.

2023-06-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6a6c7ed5cd8d: [clangd] Use include_cleaner spelling 
strategies in clangd. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152913/new/

https://reviews.llvm.org/D152913

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -75,11 +75,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -182,8 +182,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -412,22 +413,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager().getFileManager())) {
-  std::string SpelledHeader =
-  llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
-  if (!SpelledHeader.empty())
-return SpelledHeader;
-}
-  }
-  return include_cleaner::spellHeader(
-  {Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
-}
-
 IncludeCleanerFindings computeIncludeCleanerFindings(ParsedAST ) {
   // Interaction is only polished for C/CPP.
   if (AST.getLangOpts().ObjC)
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -18,6 +18,7 @@
 #include "Selection.h"
 #include "SourceCode.h"
 #include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/IncludeSpeller.h"
 #include "clang-include-cleaner/Types.h"
 #include "index/SymbolCollector.h"
 #include "support/Logger.h"
@@ -1223,7 +1224,9 @@
 // on local variables, etc.
 return;
 
-  HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H);
+  HI.Provider = include_cleaner::spellHeader(
+  {H, AST.getPreprocessor().getHeaderSearchInfo(),
+   SM.getFileEntryForID(SM.getMainFileID())});
 }
 
 // FIXME: similar functions are present in FindHeaders.cpp (symbolName)


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -75,11 +75,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -182,8 +182,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -412,22 +413,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == 

[PATCH] D152913: [clangd] Use include_cleaner spelling strategies in clangd.

2023-06-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152913

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -75,11 +75,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -182,8 +182,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -412,22 +413,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager().getFileManager())) {
-  std::string SpelledHeader =
-  llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
-  if (!SpelledHeader.empty())
-return SpelledHeader;
-}
-  }
-  return include_cleaner::spellHeader(
-  {Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
-}
-
 IncludeCleanerFindings computeIncludeCleanerFindings(ParsedAST ) {
   // Interaction is only polished for C/CPP.
   if (AST.getLangOpts().ObjC)
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -18,6 +18,7 @@
 #include "Selection.h"
 #include "SourceCode.h"
 #include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/IncludeSpeller.h"
 #include "clang-include-cleaner/Types.h"
 #include "index/SymbolCollector.h"
 #include "support/Logger.h"
@@ -1223,7 +1224,9 @@
 // on local variables, etc.
 return;
 
-  HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H);
+  HI.Provider = include_cleaner::spellHeader(
+  {H, AST.getPreprocessor().getHeaderSearchInfo(),
+   SM.getFileEntryForID(SM.getMainFileID())});
 }
 
 // FIXME: similar functions are present in FindHeaders.cpp (symbolName)


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -75,11 +75,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -182,8 +182,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -412,22 +413,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-

[PATCH] D152900: [clangd] Update symbol collector to use include-cleaner.

2023-06-14 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo updated this revision to Diff 531275.
VitaNuo added a comment.
VitaNuo updated this revision to Diff 531278.
VitaNuo added a reviewer: kadircet.
VitaNuo published this revision for review.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Remove redundant variables.


VitaNuo added a comment.

Remove redundancy.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152900

Files:
  clang-tools-extra/clangd/index/IndexAction.cpp
  clang-tools-extra/clangd/index/SymbolCollector.cpp
  clang-tools-extra/clangd/index/SymbolCollector.h
  clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -9,6 +9,7 @@
 #include "Annotations.h"
 #include "TestFS.h"
 #include "TestTU.h"
+#include "URI.h"
 #include "index/SymbolCollector.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/FileSystemOptions.h"
@@ -226,23 +227,18 @@
 
 class SymbolIndexActionFactory : public tooling::FrontendActionFactory {
 public:
-  SymbolIndexActionFactory(SymbolCollector::Options COpts,
-   CommentHandler *PragmaHandler)
-  : COpts(std::move(COpts)), PragmaHandler(PragmaHandler) {}
+  SymbolIndexActionFactory(SymbolCollector::Options COpts)
+  : COpts(std::move(COpts)) {}
 
   std::unique_ptr create() override {
 class IndexAction : public ASTFrontendAction {
 public:
   IndexAction(std::shared_ptr DataConsumer,
-  const index::IndexingOptions ,
-  CommentHandler *PragmaHandler)
-  : DataConsumer(std::move(DataConsumer)), Opts(Opts),
-PragmaHandler(PragmaHandler) {}
+  const index::IndexingOptions )
+  : DataConsumer(std::move(DataConsumer)), Opts(Opts) {}
 
   std::unique_ptr
   CreateASTConsumer(CompilerInstance , llvm::StringRef InFile) override {
-if (PragmaHandler)
-  CI.getPreprocessor().addCommentHandler(PragmaHandler);
 return createIndexingASTConsumer(DataConsumer, Opts,
  CI.getPreprocessorPtr());
   }
@@ -256,20 +252,17 @@
 private:
   std::shared_ptr DataConsumer;
   index::IndexingOptions Opts;
-  CommentHandler *PragmaHandler;
 };
 index::IndexingOptions IndexOpts;
 IndexOpts.SystemSymbolFilter =
 index::IndexingOptions::SystemSymbolFilterKind::All;
 IndexOpts.IndexFunctionLocals = true;
 Collector = std::make_shared(COpts);
-return std::make_unique(Collector, std::move(IndexOpts),
- PragmaHandler);
+return std::make_unique(Collector, std::move(IndexOpts));
   }
 
   std::shared_ptr Collector;
   SymbolCollector::Options COpts;
-  CommentHandler *PragmaHandler;
 };
 
 class SymbolCollectorTest : public ::testing::Test {
@@ -289,8 +282,7 @@
 llvm::IntrusiveRefCntPtr Files(
 new FileManager(FileSystemOptions(), InMemoryFileSystem));
 
-auto Factory = std::make_unique(
-CollectorOpts, PragmaHandler.get());
+auto Factory = std::make_unique(CollectorOpts);
 
 std::vector Args = {"symbol_collector", "-fsyntax-only",
  "-xc++", "-include", TestHeaderName};
@@ -324,7 +316,6 @@
   RefSlab Refs;
   RelationSlab Relations;
   SymbolCollector::Options CollectorOpts;
-  std::unique_ptr PragmaHandler;
 };
 
 TEST_F(SymbolCollectorTest, CollectSymbols) {
@@ -1573,9 +1564,6 @@
 
 TEST_F(SymbolCollectorTest, IWYUPragma) {
   CollectorOpts.CollectIncludePath = true;
-  CanonicalIncludes Includes;
-  PragmaHandler = collectIWYUHeaderMaps();
-  CollectorOpts.Includes = 
   const std::string Header = R"(
 // IWYU pragma: private, include the/good/header.h
 class Foo {};
@@ -1588,9 +1576,6 @@
 
 TEST_F(SymbolCollectorTest, IWYUPragmaWithDoubleQuotes) {
   CollectorOpts.CollectIncludePath = true;
-  CanonicalIncludes Includes;
-  PragmaHandler = collectIWYUHeaderMaps();
-  CollectorOpts.Includes = 
   const std::string Header = R"(
 // IWYU pragma: private, include "the/good/header.h"
 class Foo {};
@@ -1601,6 +1586,27 @@
  includeHeader("\"the/good/header.h\"";
 }
 
+TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
+  CollectorOpts.CollectIncludePath = true;
+  const std::string Header = R"cpp(#pragma once
+#include "exporter.h"
+  )cpp";
+  auto ExporterFile = testPath("exporter.h");
+  InMemoryFileSystem->addFile(
+  ExporterFile, 0, llvm::MemoryBuffer::getMemBuffer(R"cpp(#pragma once
+#include "private.h" // IWYU pragma: export
+  )cpp"));
+  auto PrivateFile = 

[PATCH] D152542: [clangd] Use include_cleaner spelling strategies in clangd.

2023-06-09 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 529974.
VitaNuo added a comment.

Remove unrelated formatting.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152542/new/

https://reviews.llvm.org/D152542

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -74,11 +74,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -198,8 +198,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -334,22 +335,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager().getFileManager())) {
-  std::string SpelledHeader =
-  llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
-  if (!SpelledHeader.empty())
-return SpelledHeader;
-}
-  }
-  return include_cleaner::spellHeader(
-  {Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
-}
-
 std::vector
 getUnused(ParsedAST ,
   const llvm::DenseSet ,
Index: clang-tools-extra/clangd/Hover.cpp
===
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -18,6 +18,7 @@
 #include "Selection.h"
 #include "SourceCode.h"
 #include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/IncludeSpeller.h"
 #include "clang-include-cleaner/Types.h"
 #include "index/SymbolCollector.h"
 #include "support/Logger.h"
@@ -1223,7 +1224,9 @@
 // on local variables, etc.
 return;
 
-  HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H);
+  HI.Provider = include_cleaner::spellHeader(
+  {H, AST.getPreprocessor().getHeaderSearchInfo(),
+   SM.getFileEntryForID(SM.getMainFileID())});
 }
 
 // FIXME: similar functions are present in FindHeaders.cpp (symbolName)


Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -74,11 +74,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -198,8 +198,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -334,22 +335,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager().getFileManager())) {
-  std::string SpelledHeader =
-  

[PATCH] D152542: [clangd] Use include_cleaner spelling strategies in clangd.

2023-06-09 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152542

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h

Index: clang-tools-extra/clangd/IncludeCleaner.h
===
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -74,11 +74,6 @@
 convertIncludes(const SourceManager ,
 const llvm::ArrayRef Includes);
 
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider);
-
 std::vector
 collectMacroReferences(ParsedAST );
 
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -198,8 +198,9 @@
   continue;
 }
 
-std::string Spelling =
-spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+std::string Spelling = include_cleaner::spellHeader(
+{SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
 
 llvm::StringRef HeaderRef{Spelling};
 bool Angled = HeaderRef.starts_with("<");
@@ -334,22 +335,6 @@
   return ConvertedIncludes;
 }
 
-std::string spellHeader(ParsedAST , const FileEntry *MainFile,
-include_cleaner::Header Provider) {
-  if (Provider.kind() == include_cleaner::Header::Physical) {
-if (auto CanonicalPath =
-getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager().getFileManager())) {
-  std::string SpelledHeader =
-  llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
-  if (!SpelledHeader.empty())
-return SpelledHeader;
-}
-  }
-  return include_cleaner::spellHeader(
-  {Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile});
-}
-
 std::vector
 getUnused(ParsedAST ,
   const llvm::DenseSet ,
@@ -459,7 +444,8 @@
   return {std::move(UnusedIncludes), std::move(MissingIncludes)};
 }
 
-std::optional removeAllUnusedIncludes(llvm::ArrayRef UnusedIncludes) {
+std::optional
+removeAllUnusedIncludes(llvm::ArrayRef UnusedIncludes) {
   if (UnusedIncludes.empty())
 return std::nullopt;
 
@@ -468,8 +454,8 @@
   for (const auto  : UnusedIncludes) {
 assert(Diag.Fixes.size() == 1 && "Expected exactly one fix.");
 RemoveAll.Edits.insert(RemoveAll.Edits.end(),
- Diag.Fixes.front().Edits.begin(),
- Diag.Fixes.front().Edits.end());
+   Diag.Fixes.front().Edits.begin(),
+   Diag.Fixes.front().Edits.end());
   }
 
   // TODO(hokein): emit a suitable text for the label.
@@ -497,7 +483,7 @@
   llvm::StringMap Edits;
   for (const auto  : MissingIncludeDiags) {
 assert(Diag.Fixes.size() == 1 && "Expected exactly one fix.");
-for (const auto& Edit : Diag.Fixes.front().Edits) {
+for (const auto  : Diag.Fixes.front().Edits) {
   Edits.try_emplace(Edit.newText, Edit);
 }
   }
@@ -517,7 +503,7 @@
   }
   return AddAllMissing;
 }
-Fix fixAll(const Fix& RemoveAllUnused, const Fix& AddAllMissing) {
+Fix fixAll(const Fix , const Fix ) {
   Fix FixAll;
   FixAll.Message = "fix all includes";
 
@@ -526,22 +512,23 @@
   for (const auto  : AddAllMissing.Edits)
 FixAll.Edits.push_back(F);
 
-  for (const auto& A : RemoveAllUnused.Annotations)
+  for (const auto  : RemoveAllUnused.Annotations)
 FixAll.Annotations.push_back(A);
-  for (const auto& A : AddAllMissing.Annotations)
+  for (const auto  : AddAllMissing.Annotations)
 FixAll.Annotations.push_back(A);
   return FixAll;
 }
 
-std::vector generateIncludeCleanerDiagnostic(
-ParsedAST , const IncludeCleanerFindings ,
-llvm::StringRef Code) {
+std::vector
+generateIncludeCleanerDiagnostic(ParsedAST ,
+ const IncludeCleanerFindings ,
+ llvm::StringRef Code) {
   std::vector UnusedIncludes = generateUnusedIncludeDiagnostics(
   AST.tuPath(), Findings.UnusedIncludes, Code);
   std::optional RemoveAllUnused = removeAllUnusedIncludes(UnusedIncludes);
 
-  std::vector MissingIncludeDiags = generateMissingIncludeDiagnostics(
-  AST, Findings.MissingIncludes, Code);
+  std::vector MissingIncludeDiags =
+  generateMissingIncludeDiagnostics(AST, Findings.MissingIncludes, Code);
   std::optional AddAllMissing = 

[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-05 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG90c5fe98: [include-cleaner] Allow multiple strategies 
for spelling includes. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
  
clang-tools-extra/include-cleaner/include/clang-include-cleaner/IncludeSpeller.h
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/lib/CMakeLists.txt
  clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
  clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
  clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
@@ -0,0 +1,78 @@
+//===--- IncludeSpellerTest.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Types.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Testing/TestAST.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Path.h"
+#include "gtest/gtest.h"
+#include 
+#include 
+namespace clang::include_cleaner {
+namespace {
+
+const char *testRoot() {
+#ifdef _WIN32
+  return "C:\\include-cleaner-test";
+#else
+  return "/include-cleaner-test";
+#endif
+}
+
+std::string testPath(llvm::StringRef File) {
+  assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
+
+  llvm::SmallString<32> NativeFile = File;
+  llvm::sys::path::native(NativeFile, llvm::sys::path::Style::native);
+  llvm::SmallString<32> Path;
+  llvm::sys::path::append(Path, llvm::sys::path::Style::native, testRoot(),
+  NativeFile);
+  return std::string(Path.str());
+}
+
+class DummyIncludeSpeller : public IncludeSpeller {
+public:
+  std::string operator()(const IncludeSpeller::Input ) const override {
+llvm::StringRef AbsolutePath = Input.H.physical()->tryGetRealPathName();
+std::string RootWithSeparator{testRoot()};
+RootWithSeparator += llvm::sys::path::get_separator();
+if (!AbsolutePath.consume_front(llvm::StringRef{RootWithSeparator}))
+  return "";
+return "\"" + AbsolutePath.str() + "\"";
+  }
+};
+
+TEST(IncludeSpeller, IsRelativeToTestRoot) {
+  TestInputs Inputs;
+
+  Inputs.ExtraArgs.push_back("-isystemdir");
+
+  Inputs.ExtraFiles[testPath("foo.h")] = "";
+  Inputs.ExtraFiles["dir/header.h"] = "";
+  TestAST AST{Inputs};
+
+  auto  = AST.fileManager();
+  auto  = AST.preprocessor().getHeaderSearchInfo();
+  const auto *MainFile = AST.sourceManager().getFileEntryForID(
+  AST.sourceManager().getMainFileID());
+
+  EXPECT_EQ("\"foo.h\"", spellHeader({Header{*FM.getFile(testPath("foo.h"))},
+  HS, MainFile}));
+  EXPECT_EQ("",
+spellHeader({Header{*FM.getFile("dir/header.h")}, HS, MainFile}));
+}
+
+IncludeSpellingStrategy::Add
+Speller("dummy", "Dummy Include Speller");
+
+} // namespace
+} // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
===
--- clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -7,6 +7,7 @@
 add_unittest(ClangIncludeCleanerUnitTests ClangIncludeCleanerTests
   AnalysisTest.cpp
   FindHeadersTest.cpp
+  IncludeSpellerTest.cpp
   LocateSymbolTest.cpp
   RecordTest.cpp
   TypesTest.cpp
Index: clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
@@ -0,0 +1,66 @@
+//===--- IncludeSpeller.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+

[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-05 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 528341.
VitaNuo marked 4 inline comments as done.
VitaNuo added a comment.

Address the comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
  
clang-tools-extra/include-cleaner/include/clang-include-cleaner/IncludeSpeller.h
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/lib/CMakeLists.txt
  clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
  clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
  clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
@@ -0,0 +1,78 @@
+//===--- IncludeSpellerTest.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Types.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Testing/TestAST.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Path.h"
+#include "gtest/gtest.h"
+#include 
+#include 
+namespace clang::include_cleaner {
+namespace {
+
+const char *testRoot() {
+#ifdef _WIN32
+  return "C:\\include-cleaner-test";
+#else
+  return "/include-cleaner-test";
+#endif
+}
+
+std::string testPath(llvm::StringRef File) {
+  assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
+
+  llvm::SmallString<32> NativeFile = File;
+  llvm::sys::path::native(NativeFile, llvm::sys::path::Style::native);
+  llvm::SmallString<32> Path;
+  llvm::sys::path::append(Path, llvm::sys::path::Style::native, testRoot(),
+  NativeFile);
+  return std::string(Path.str());
+}
+
+class DummyIncludeSpeller : public IncludeSpeller {
+public:
+  std::string operator()(const IncludeSpeller::Input ) const override {
+llvm::StringRef AbsolutePath = Input.H.physical()->tryGetRealPathName();
+std::string RootWithSeparator{testRoot()};
+RootWithSeparator += llvm::sys::path::get_separator();
+if (!AbsolutePath.consume_front(llvm::StringRef{RootWithSeparator}))
+  return "";
+return "\"" + AbsolutePath.str() + "\"";
+  }
+};
+
+TEST(IncludeSpeller, IsRelativeToTestRoot) {
+  TestInputs Inputs;
+
+  Inputs.ExtraArgs.push_back("-isystemdir");
+
+  Inputs.ExtraFiles[testPath("foo.h")] = "";
+  Inputs.ExtraFiles["dir/header.h"] = "";
+  TestAST AST{Inputs};
+
+  auto  = AST.fileManager();
+  auto  = AST.preprocessor().getHeaderSearchInfo();
+  const auto *MainFile = AST.sourceManager().getFileEntryForID(
+  AST.sourceManager().getMainFileID());
+
+  EXPECT_EQ("\"foo.h\"", spellHeader({Header{*FM.getFile(testPath("foo.h"))},
+  HS, MainFile}));
+  EXPECT_EQ("",
+spellHeader({Header{*FM.getFile("dir/header.h")}, HS, MainFile}));
+}
+
+IncludeSpellingStrategy::Add
+Speller("dummy", "Dummy Include Speller");
+
+} // namespace
+} // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
===
--- clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -7,6 +7,7 @@
 add_unittest(ClangIncludeCleanerUnitTests ClangIncludeCleanerTests
   AnalysisTest.cpp
   FindHeadersTest.cpp
+  IncludeSpellerTest.cpp
   LocateSymbolTest.cpp
   RecordTest.cpp
   TypesTest.cpp
Index: clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
@@ -0,0 +1,66 @@
+//===--- IncludeSpeller.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Types.h"
+#include 

[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-05 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the comments!




Comment at: clang-tools-extra/clangd/Hover.cpp:1225
 
-  HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H);
+  HI.Provider = include_cleaner::spellHeader(
+  {H, AST.getPreprocessor().getHeaderSearchInfo(),

hokein wrote:
> we probably need to add a `IncludeSpeller.h` #include insertion for this 
> file, and probably for `clangd/IncludeCleaner.cpp` as well
Yeah also to the `IncludeCleanerCheck.cpp` now that I've rebased to main.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-05 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 528315.
VitaNuo marked 11 inline comments as done.
VitaNuo added a comment.
Herald added subscribers: PiotrZSL, carlosgalvezp.
Herald added a reviewer: njames93.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

Files:
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
  
clang-tools-extra/include-cleaner/include/clang-include-cleaner/IncludeSpeller.h
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/lib/CMakeLists.txt
  clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
  clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
  clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
@@ -0,0 +1,78 @@
+//===--- IncludeSpellerTest.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Types.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Testing/TestAST.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Path.h"
+#include "gtest/gtest.h"
+#include 
+#include 
+namespace clang::include_cleaner {
+namespace {
+
+const char *testRoot() {
+#ifdef _WIN32
+  return "C:\\include-cleaner-test";
+#else
+  return "/include-cleaner-test";
+#endif
+}
+
+std::string testPath(llvm::StringRef File) {
+  assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
+
+  llvm::SmallString<32> NativeFile = File;
+  llvm::sys::path::native(NativeFile, llvm::sys::path::Style::native);
+  llvm::SmallString<32> Path;
+  llvm::sys::path::append(Path, llvm::sys::path::Style::native, testRoot(),
+  NativeFile);
+  return std::string(Path.str());
+}
+
+class DummyIncludeSpeller : public IncludeSpeller {
+public:
+  std::string operator()(const IncludeSpeller::Input ) const override {
+llvm::StringRef AbsolutePath = Input.H.physical()->tryGetRealPathName();
+std::string RootWithSeparator{testRoot()};
+RootWithSeparator += llvm::sys::path::get_separator();
+if (!AbsolutePath.consume_front(llvm::StringRef{RootWithSeparator}))
+  return "";
+return "\"" + AbsolutePath.str() + "\"";
+  }
+};
+
+TEST(IncludeSpeller, IsRelativeToTestRoot) {
+  TestInputs Inputs;
+
+  Inputs.ExtraArgs.push_back("-isystemdir");
+
+  Inputs.ExtraFiles[testPath("foo.h")] = "";
+  Inputs.ExtraFiles["dir/header.h"] = "";
+  TestAST AST{Inputs};
+
+  auto  = AST.fileManager();
+  auto  = AST.preprocessor().getHeaderSearchInfo();
+  const auto *MainFile = AST.sourceManager().getFileEntryForID(
+  AST.sourceManager().getMainFileID());
+
+  EXPECT_EQ("\"foo.h\"", spellHeader({Header{*FM.getFile(testPath("foo.h"))},
+  HS, MainFile}));
+  EXPECT_EQ("",
+spellHeader({Header{*FM.getFile("dir/header.h")}, HS, MainFile}));
+}
+
+IncludeSpellingStrategy::Add
+Speller("dummy", "Dummy Include Speller");
+
+} // namespace
+} // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
===
--- clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -7,6 +7,7 @@
 add_unittest(ClangIncludeCleanerUnitTests ClangIncludeCleanerTests
   AnalysisTest.cpp
   FindHeadersTest.cpp
+  IncludeSpellerTest.cpp
   LocateSymbolTest.cpp
   RecordTest.cpp
   TypesTest.cpp
Index: clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
@@ -0,0 +1,66 @@
+//===--- IncludeSpeller.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include 

[PATCH] D148793: [clang-tidy] Implement an include-cleaner check.

2023-06-02 Thread Viktoriia Bakalova via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc28506ba4b69: [clang-tidy] Implement an include-cleaner 
check. (authored by VitaNuo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148793/new/

https://reviews.llvm.org/D148793

Files:
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.cpp
  clang-tools-extra/clang-tidy/misc/IncludeCleanerCheck.h
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/clangd/TidyProvider.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/misc/include-cleaner.rst
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
  clang-tools-extra/include-cleaner/lib/Record.cpp
  clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/bar.h
  clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/baz.h
  clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/foo.h
  clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/private.h
  clang-tools-extra/test/clang-tidy/checkers/misc/include-cleaner.cpp
  clang-tools-extra/test/clang-tidy/checkers/misc/system/string.h
  clang-tools-extra/test/clang-tidy/checkers/misc/system/vector.h
  clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
  clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp

Index: clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
===
--- /dev/null
+++ clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
@@ -0,0 +1,236 @@
+//===--- IncludeCleanerTest.cpp - clang-tidy -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ClangTidyDiagnosticConsumer.h"
+#include "ClangTidyOptions.h"
+#include "ClangTidyTest.h"
+#include "misc/IncludeCleanerCheck.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Regex.h"
+#include "gtest/gtest.h"
+#include 
+
+#include 
+#include 
+
+using namespace clang::tidy::misc;
+
+namespace clang {
+namespace tidy {
+namespace test {
+namespace {
+
+std::string
+appendPathFileSystemIndependent(std::initializer_list Segments) {
+  llvm::SmallString<32> Result;
+  for (const auto  : Segments)
+llvm::sys::path::append(Result, llvm::sys::path::Style::native, Segment);
+  return std::string(Result.str());
+}
+
+TEST(IncludeCleanerCheckTest, BasicUnusedIncludes) {
+  const char *PreCode = R"(
+#include "bar.h"
+#include 
+#include "bar.h"
+)";
+  const char *PostCode = "\n";
+
+  std::vector Errors;
+  EXPECT_EQ(PostCode, runCheckOnCode(
+  PreCode, , "file.cpp", std::nullopt,
+  ClangTidyOptions(), {{"bar.h", ""}, {"vector", ""}}));
+}
+
+TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
+  const char *PreCode = R"(
+#include "bar.h"
+#include "foo/qux.h"
+#include "baz/qux/qux.h"
+#include 
+)";
+
+  const char *PostCode = R"(
+#include "bar.h"
+#include "foo/qux.h"
+#include 
+)";
+
+  std::vector Errors;
+  ClangTidyOptions Opts;
+  Opts.CheckOptions["IgnoreHeaders"] = llvm::StringRef{llvm::formatv(
+  "bar.h;{0};{1};vector",
+  llvm::Regex::escape(appendPathFileSystemIndependent({"foo", "qux.h"})),
+  llvm::Regex::escape(appendPathFileSystemIndependent({"baz", "qux"})))};
+  EXPECT_EQ(
+  PostCode,
+  runCheckOnCode(
+  PreCode, , "file.cpp", std::nullopt, Opts,
+  {{"bar.h", ""},
+   {"vector", ""},
+   {appendPathFileSystemIndependent({"foo", "qux.h"}), ""},
+   {appendPathFileSystemIndependent({"baz", "qux", "qux.h"}), ""}}));
+}
+
+TEST(IncludeCleanerCheckTest, BasicMissingIncludes) {
+  const char *PreCode = R"(
+#include "bar.h"
+
+int BarResult = bar();
+int BazResult = baz();
+)";
+  const char *PostCode = R"(
+#include "bar.h"
+#include "baz.h"
+
+int BarResult = bar();
+int BazResult = baz();
+)";
+
+  std::vector Errors;
+  EXPECT_EQ(PostCode,
+runCheckOnCode(
+PreCode, , "file.cpp", std::nullopt, ClangTidyOptions(),
+{{"bar.h", R"(#pragma once
+  #include "baz.h"
+  int bar();
+   )"},
+ {"baz.h", R"(#pragma once
+  int baz();
+   )"}}));
+}
+
+TEST(IncludeCleanerCheckTest, SuppressMissingIncludes) {
+  const char *PreCode = R"(
+#include "bar.h"
+
+int BarResult = bar();
+int BazResult = baz();
+int QuxResult = qux();
+)";

[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-02 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo added a comment.

Thanks for the comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D150185: [include-cleaner] Allow multiple strategies for spelling includes.

2023-06-02 Thread Viktoriia Bakalova via Phabricator via cfe-commits
VitaNuo updated this revision to Diff 527872.
VitaNuo marked 6 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150185/new/

https://reviews.llvm.org/D150185

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/IncludeCleaner.cpp
  clang-tools-extra/clangd/IncludeCleaner.h
  clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
  
clang-tools-extra/include-cleaner/include/clang-include-cleaner/IncludeSpeller.h
  clang-tools-extra/include-cleaner/lib/Analysis.cpp
  clang-tools-extra/include-cleaner/lib/CMakeLists.txt
  clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
  clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
  clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp

Index: clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/unittests/IncludeSpellerTest.cpp
@@ -0,0 +1,78 @@
+//===--- IncludeSpellerTest.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Types.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Testing/TestAST.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Path.h"
+#include "gtest/gtest.h"
+#include 
+#include 
+namespace clang::include_cleaner {
+namespace {
+
+const char *testRoot() {
+#ifdef _WIN32
+  return "C:\\include-cleaner-test";
+#else
+  return "/include-cleaner-test";
+#endif
+}
+
+std::string testPath(llvm::StringRef File) {
+  assert(llvm::sys::path::is_relative(File) && "FileName should be relative");
+
+  llvm::SmallString<32> NativeFile = File;
+  llvm::sys::path::native(NativeFile, llvm::sys::path::Style::native);
+  llvm::SmallString<32> Path;
+  llvm::sys::path::append(Path, llvm::sys::path::Style::native, testRoot(),
+  NativeFile);
+  return std::string(Path.str());
+}
+
+class DummyIncludeSpeller : public IncludeSpeller {
+public:
+  std::string operator()(const IncludeSpeller::Input ) const override {
+llvm::StringRef AbsolutePath = Input.H.physical()->tryGetRealPathName();
+std::string RootWithSeparator{testRoot()};
+RootWithSeparator += llvm::sys::path::get_separator();
+if (!AbsolutePath.consume_front(llvm::StringRef{RootWithSeparator}))
+  return "";
+return "\"" + AbsolutePath.str() + "\"";
+  }
+};
+
+TEST(IncludeSpeller, IsRelativeToTestRoot) {
+  TestInputs Inputs;
+
+  Inputs.ExtraArgs.push_back("-isystemdir");
+
+  Inputs.ExtraFiles[testPath("foo.h")] = "";
+  Inputs.ExtraFiles["dir/header.h"] = "";
+  TestAST AST{Inputs};
+
+  auto  = AST.fileManager();
+  auto  = AST.preprocessor().getHeaderSearchInfo();
+  const auto *MainFile = AST.sourceManager().getFileEntryForID(
+  AST.sourceManager().getMainFileID());
+
+  EXPECT_EQ("\"foo.h\"", spellHeader({Header{*FM.getFile(testPath("foo.h"))},
+  HS, MainFile}));
+  EXPECT_EQ("",
+spellHeader({Header{*FM.getFile("dir/header.h")}, HS, MainFile}));
+}
+
+IncludeSpellingStrategy::Add
+Speller("dummy", "Dummy Include Speller");
+
+} // namespace
+} // namespace clang::include_cleaner
Index: clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
===
--- clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
+++ clang-tools-extra/include-cleaner/unittests/CMakeLists.txt
@@ -7,6 +7,7 @@
 add_unittest(ClangIncludeCleanerUnitTests ClangIncludeCleanerTests
   AnalysisTest.cpp
   FindHeadersTest.cpp
+  IncludeSpellerTest.cpp
   LocateSymbolTest.cpp
   RecordTest.cpp
   TypesTest.cpp
Index: clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
===
--- /dev/null
+++ clang-tools-extra/include-cleaner/lib/IncludeSpeller.cpp
@@ -0,0 +1,68 @@
+//===--- IncludeSpeller.cpp===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "clang-include-cleaner/IncludeSpeller.h"
+#include "clang-include-cleaner/Types.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/ErrorHandling.h"
+#include 

  1   2   3   4   5   >