Author: Access
Date: 2026-06-09T16:29:57+08:00
New Revision: 620cdfb1d1000e9de4cdeb35dfa60ff0299d925e

URL: 
https://github.com/llvm/llvm-project/commit/620cdfb1d1000e9de4cdeb35dfa60ff0299d925e
DIFF: 
https://github.com/llvm/llvm-project/commit/620cdfb1d1000e9de4cdeb35dfa60ff0299d925e.diff

LOG: [clangd][Mangler] Drop unknown compile options (#200001)

this pr remove the options that not work for clang, which will make
compile failed for clang++, and this will make modules completions of
clangd work. And we will also log the unsupported options

Added: 
    

Modified: 
    clang-tools-extra/clangd/CompileCommands.cpp
    clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/CompileCommands.cpp 
b/clang-tools-extra/clangd/CompileCommands.cpp
index 4eda330716f21..f8bc9a9ca81fd 100644
--- a/clang-tools-extra/clangd/CompileCommands.cpp
+++ b/clang-tools-extra/clangd/CompileCommands.cpp
@@ -273,6 +273,19 @@ void CommandMangler::operator()(tooling::CompileCommand 
&Command,
       SawInput(Cmd[I]);
     Cmd.resize(DashDashIndex);
   }
+
+  llvm::SmallVector<const char *, 16> UnknownArgs;
+
+  for (auto *UnknownArg : ArgList.filtered(options::OPT_UNKNOWN)) {
+    UnknownArgs.push_back(UnknownArg->getValue());
+    IndicesToDrop.push_back(UnknownArg->getIndex());
+  }
+
+  if (!UnknownArgs.empty()) {
+    log("Warning: detected unsupported options '{0}'",
+        llvm::join(UnknownArgs, ", "));
+  }
+
   llvm::sort(IndicesToDrop);
   for (unsigned Idx : llvm::reverse(IndicesToDrop))
     // +1 to account for the executable name in Cmd[0] that

diff  --git a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp 
b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
index 0c1e0348f68a5..228a4da6969ce 100644
--- a/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
@@ -525,6 +525,17 @@ TEST(CommandMangler, RespectsOriginalSysroot) {
                 Not(HasSubstr(testPath("fake/sysroot"))));
   }
 }
+TEST(CommandMangler, ClangUnknownArgs) {
+  // Check that clang++ will drop unknown flags
+  const auto Mangler = CommandMangler::forTests();
+  tooling::CompileCommand Cmd;
+  Cmd.CommandLine = {"clang++", "-std=c++23", "--unknown-flag",
+                     "--unknown-option=abcd"};
+  Mangler(Cmd, "foo.cc");
+  EXPECT_THAT(Cmd.CommandLine, Not(Contains("--unknown-flag")));
+  EXPECT_THAT(Cmd.CommandLine, Not(Contains("--unknown-option=abcd")));
+  EXPECT_THAT(Cmd.CommandLine, Contains("-std=c++23"));
+}
 
 TEST(CommandMangler, StdLatestFlag) {
   const auto Mangler = CommandMangler::forTests();


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to