[PATCH] D138546: Clangd: Preserve target flags in system includes extractor

Christopher Sauer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 23 00:31:23 PST 2022


cpsauer created this revision.
cpsauer added reviewers: nridge, sammccall, kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
cpsauer requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This preserves --target and -target flags in the system includes extractor, which is needed for cross-compiling to, e.g. Android, since the target flags can influence the default include paths.

Note that, like isysroot (which is handled incorrectly above and elsewhere), the target flag doesn't fit cleanly into the ArgsToPreserve abstraction and does indeed have a different number of - in its = and non = forms. (ref <https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-isysroot-dir>) There are plenty of bugs in this file, but this is an incremental improvement.

For more context, please see https://github.com/clangd/clangd/issues/1378

Thanks for all you do, wonderful LLVM folks :)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138546

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


Index: clang-tools-extra/clangd/SystemIncludeExtractor.cpp
===================================================================
--- clang-tools-extra/clangd/SystemIncludeExtractor.cpp
+++ clang-tools-extra/clangd/SystemIncludeExtractor.cpp
@@ -184,13 +184,18 @@
   const llvm::StringRef FlagsToPreserve[] = {
       "-nostdinc", "--no-standard-includes", "-nostdinc++", "-nobuiltininc"};
   // Preserves these flags and their values, either as separate args or with an
-  // equalsbetween them
+  // equals between them
   const llvm::StringRef ArgsToPreserve[] = {"--sysroot", "-isysroot"};
 
   for (size_t I = 0, E = CommandLine.size(); I < E; ++I) {
     llvm::StringRef Arg = CommandLine[I];
     if (llvm::is_contained(FlagsToPreserve, Arg)) {
       Args.push_back(Arg);
+    } else if (Arg.startswith("--target=")) {
+      Args.push_back(Arg);
+    } else if (I + 1 < E && Arg.equals("-target")) {
+      Args.push_back(CommandLine[I]);
+      Args.push_back(CommandLine[++I]);
     } else {
       const auto *Found =
           llvm::find_if(ArgsToPreserve, [&Arg](llvm::StringRef S) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138546.477400.patch
Type: text/x-patch
Size: 1096 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221123/d68e864b/attachment-0001.bin>


More information about the cfe-commits mailing list