Skip to content

Commit

Permalink
[clangd] Support -stdlib flags in SystemIncludeExtractor. (#69283)
Browse files Browse the repository at this point in the history
The `--stdlib` flag can affect the system headers used by `clang` during
compilation. By default, `clang` will use the platform-installed C++
standard headers, but with `--stdlib=libc++`, `clang` can use headers
included in the distribution for its `libc++` implementation.

Prior to this patch, if `compile_commands.json` specified
`-stdlib=libc++` or an equivalent form and `--query-driver` took effect,
`clangd` would ignore `stdlib` and index based on the platform's
headers. When these mismatch, e.g. due to version differences,
`clangd`'s completions and the actual compilation can differ.

fixes clangd/clangd#1784

---------

Co-authored-by: Chris Carlon <cjc25@cjc25.com>
  • Loading branch information
cjc25 and cjc25 committed Oct 24, 2023
1 parent 2f3d4f6 commit 3935a29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions clang-tools-extra/clangd/SystemIncludeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ struct DriverArgs {
std::string Sysroot;
std::string ISysroot;
std::string Target;
std::string Stdlib;

bool operator==(const DriverArgs &RHS) const {
return std::tie(Driver, StandardIncludes, StandardCXXIncludes, Lang,
Sysroot, ISysroot, Target) ==
Sysroot, ISysroot, Target, Stdlib) ==
std::tie(RHS.Driver, RHS.StandardIncludes, RHS.StandardCXXIncludes,
RHS.Lang, RHS.Sysroot, RHS.ISysroot, RHS.Target);
RHS.Lang, RHS.Sysroot, RHS.ISysroot, RHS.Target,
RHS.Stdlib);
}

DriverArgs(const tooling::CompileCommand &Cmd, llvm::StringRef File) {
Expand Down Expand Up @@ -136,6 +138,13 @@ struct DriverArgs {
} else if (Arg.consume_front("-target")) {
if (Arg.empty() && I + 1 < E)
Target = Cmd.CommandLine[I + 1];
} else if (Arg.consume_front("--stdlib")) {
if (Arg.consume_front("="))
Stdlib = Arg.str();
else if (Arg.empty() && I + 1 < E)
Stdlib = Cmd.CommandLine[I + 1];
} else if (Arg.consume_front("-stdlib=")) {
Stdlib = Arg.str();
}
}

Expand Down Expand Up @@ -175,6 +184,8 @@ struct DriverArgs {
Args.append({"-isysroot", ISysroot});
if (!Target.empty())
Args.append({"-target", Target});
if (!Stdlib.empty())
Args.append({"--stdlib", Stdlib});
return Args;
}

Expand Down Expand Up @@ -206,6 +217,8 @@ template <> struct DenseMapInfo<DriverArgs> {
Val.Lang,
Val.Sysroot,
Val.ISysroot,
Val.Target,
Val.Stdlib,
});
}
static bool isEqual(const DriverArgs &LHS, const DriverArgs &RHS) {
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clangd/test/system-include-extractor.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# RUN: echo '[ -z "${args##*"--sysroot /my/sysroot/path"*}" ] || exit' >> %t.dir/bin/my_driver.sh
# RUN: echo '[ -z "${args##*"-isysroot /isysroot"*}" ] || exit' >> %t.dir/bin/my_driver.sh
# RUN: echo '[ -z "${args##*"-target arm-linux-gnueabihf"*}" ] || exit' >> %t.dir/bin/my_driver.sh
# RUN: echo '[ -z "${args##*"--stdlib libc++"*}" ] || exit' >> %t.dir/bin/my_driver.sh
# RUN: echo 'echo line to ignore >&2' >> %t.dir/bin/my_driver.sh
# RUN: echo 'printf "Target: arm-linux-gnueabihf\r\n" >&2' >> %t.dir/bin/my_driver.sh
# RUN: echo 'printf "#include <...> search starts here:\r\n" >&2' >> %t.dir/bin/my_driver.sh
Expand All @@ -37,7 +38,7 @@

# Generate a compile_commands.json that will query the mock driver we've
# created. Which should add a.h and b.h into include search path.
# RUN: echo '[{"directory": "%/t.dir", "command": "my_driver.sh the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json
# RUN: echo '[{"directory": "%/t.dir", "command": "my_driver.sh the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot -stdlib=libc++", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json

# RUN: sed -e "s|INPUT_DIR|%/t.dir|g" %s > %t.test.1
# On Windows, we need the URI in didOpen to look like "uri":"file:///C:/..."
Expand Down Expand Up @@ -75,7 +76,7 @@
{"jsonrpc":"2.0","method":"exit"}

# Generate a different compile_commands.json which does not point to the mock driver
# RUN: echo '[{"directory": "%/t.dir", "command": "gcc the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json
# RUN: echo '[{"directory": "%/t.dir", "command": "gcc the-file.cpp --target=arm-linux-gnueabihf -nostdinc --sysroot /my/sysroot/path -isysroot/isysroot -stdlib=libc++", "file": "the-file.cpp"}]' > %t.dir/compile_commands.json

# Generate a clangd config file which points to the mock driver instead
# RUN: echo 'CompileFlags:' > %t.dir/.clangd
Expand Down

0 comments on commit 3935a29

Please sign in to comment.