serge-sans-paille created this revision.
serge-sans-paille added reviewers: kbobyrev, sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman, mgorny.
Herald added a project: All.
serge-sans-paille requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This is just a wrapper around clangd include cleaner's capability to be used in 
CLI mode.

I'm not sure why include-cleaner wasn't integrated in clang-tidy, but I suspect 
there's a good reason.
This revision is a support for discussing a potential tool that would leverage 
on include cleaner from clangd: it would be great to be able to run such a tool 
on LLVM database and make sure we don't regress.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121593

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/include-cleaner/CMakeLists.txt
  clang-tools-extra/clangd/include-cleaner/ClangIncludeCleanerMain.cpp

Index: clang-tools-extra/clangd/include-cleaner/ClangIncludeCleanerMain.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/include-cleaner/ClangIncludeCleanerMain.cpp
@@ -0,0 +1,69 @@
+#include "ClangdServer.h"
+#include "support/Logger.h"
+#include "llvm/Support/Path.h"
+
+using namespace clang::clangd;
+
+struct report {
+  PathRef File;
+  void operator()(llvm::Expected<std::vector<const Inclusion *>> C) {
+    if (C) {
+      auto &Added = C.get();
+      if (Added.empty())
+        return;
+      llvm::errs() << "Found " << Added.size() << " unused include"
+                   << (Added.size() == 1 ? " " : "s ") << "in: " << File
+                   << "\n";
+      for (auto const *I : Added) {
+        llvm::outs() << "- " << I->Resolved << "\n";
+      }
+    } else
+      llvm::errs() << C.takeError();
+  }
+};
+
+class NullLogger : public Logger {
+  void log(Level, const char *Fmt,
+           const llvm::formatv_object_base &Message) final {}
+};
+
+int main(int argc, char **argv) {
+  if (argc < 2) {
+    llvm::errs()
+        << "usage: clang-include-cleaner compile_commands_dir files...\n";
+    return 1;
+  }
+  RealThreadsafeFS TFS;
+  DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS);
+  llvm::SmallString<64> CCD;
+  auto errc = llvm::sys::fs::real_path(argv[1], CCD);
+  if (errc) {
+    llvm::errs() << argv[1] << ": " << errc.message() << "\n";
+    return 2;
+  }
+  CDBOpts.CompileCommandsDir = CCD.str().str();
+  DirectoryBasedGlobalCompilationDatabase CDB(CDBOpts);
+
+  StreamLogger SLog(llvm::errs(), Logger::Error);
+  clang::clangd::LoggingSession LoggingSession(SLog);
+
+  ClangdServer::Options CSOpts;
+  ClangdServer CS(CDB, TFS, CSOpts);
+
+  for (int i = 2; i < argc; ++i) {
+    PathRef File = argv[i];
+    auto MB = llvm::MemoryBuffer::getFile(File);
+    if (!MB) {
+      llvm::errs() << "failed to open " << File;
+      return 1;
+    }
+    CS.addDocument(File, MB.get()->getBuffer());
+    CS.findUnusedIncludes(File, report{File});
+
+    // We're using clangd in synchronous mode
+    while (CS.blockUntilIdleForTest(.1))
+      ;
+  }
+
+  return 0;
+}
Index: clang-tools-extra/clangd/include-cleaner/CMakeLists.txt
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/include-cleaner/CMakeLists.txt
@@ -0,0 +1,29 @@
+add_clang_tool(clang-include-cleaner
+  ClangIncludeCleanerMain.cpp
+  )
+
+set(LLVM_LINK_COMPONENTS
+  support
+  )
+
+clang_target_link_libraries(clang-include-cleaner
+  PRIVATE
+  clangAST
+  clangBasic
+  clangFormat
+  clangFrontend
+  clangLex
+  clangSema
+  clangTooling
+  clangToolingCore
+  clangToolingRefactoring
+  clangToolingSyntax
+  )
+
+target_link_libraries(clang-include-cleaner
+  PRIVATE
+
+  clangDaemon
+  clangdRemoteIndex
+  clangdSupport
+  )
Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -180,6 +180,7 @@
   add_subdirectory(fuzzer)
 endif()
 add_subdirectory(tool)
+add_subdirectory(include-cleaner)
 add_subdirectory(indexer)
 
 if (LLVM_INCLUDE_BENCHMARKS)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to