Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Driver] Fix detection of libc++ with empty sysroot. #66947

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,8 @@ Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
const Driver &D = getDriver();
std::string SysRoot = computeSysRoot();
if (SysRoot.empty())
SysRoot = llvm::sys::path::get_separator();

auto AddIncludePath = [&](StringRef Path, bool TargetDirRequired = false) {
std::string Version = detectLibcxxVersion(Path);
Expand Down
48 changes: 48 additions & 0 deletions clang/unittests/Driver/ToolChainTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
#include "clang/Driver/Driver.h"
#include "clang/Frontend/CompilerInstance.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>

Expand Down Expand Up @@ -316,6 +318,52 @@ TEST(ToolChainTest, VFSSolarisMultiGCCInstallation) {
}
}

MATCHER_P(jobHasArgs, Substr, "") {
const driver::Command &C = arg;
std::string Args = "";
llvm::ListSeparator Sep(" ");
for (const char *Arg : C.getArguments()) {
Args += Sep;
Args += Arg;
}
if (is_style_windows(llvm::sys::path::Style::native))
std::replace(Args.begin(), Args.end(), '\\', '/');
if (llvm::StringRef(Args).contains(Substr))
return true;
*result_listener << "whose args are '" << Args << "'";
smeenai marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

TEST(ToolChainTest, VFSGnuLibcxxPathNoSysroot) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();

IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
new llvm::vfs::InMemoryFileSystem);

const char *EmptyFiles[] = {
"foo.cpp",
"/bin/clang",
"/usr/include/c++/v1/cstdio",
};

for (const char *Path : EmptyFiles)
InMemoryFileSystem->addFile(Path, 0,
llvm::MemoryBuffer::getMemBuffer("\n"));

{
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
Driver TheDriver("/bin/clang", "x86_64-unknown-linux-gnu", Diags,
"clang LLVM compiler", InMemoryFileSystem);
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(
{"/bin/clang", "-fsyntax-only", "-stdlib=libc++", "foo.cpp"}));
ASSERT_TRUE(C);
EXPECT_THAT(C->getJobs(), testing::ElementsAre(jobHasArgs(
"-internal-isystem /usr/include/c++/v1")));
}
}

TEST(ToolChainTest, DefaultDriverMode) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();

Expand Down