From 1d533d0c846e3f9932990fde0a93320378df7af9 Mon Sep 17 00:00:00 2001 From: Nikolai Kholiavin Date: Fri, 2 Jun 2023 18:31:00 +0300 Subject: [PATCH] [OHOS][Driver] Use VFS instead of real filesystem for sysroot search OHOS ToolChain class now uses driver's VFS instead of sys::fs methods to look for sysroot paths, otherwise relative ones can be wrongly excluded as non- existent from the wrong path. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I6Y6DN Change-Id: I7923e772e454fb619983c578b6512b2b327ad2ae Signed-off-by: Nikolai Kholiavin --- clang/lib/Driver/ToolChains/OHOS.cpp | 4 +- clang/unittests/Driver/CMakeLists.txt | 1 + clang/unittests/Driver/OHOSTCTest.cpp | 78 +++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 clang/unittests/Driver/OHOSTCTest.cpp diff --git a/clang/lib/Driver/ToolChains/OHOS.cpp b/clang/lib/Driver/ToolChains/OHOS.cpp index e12b34f15dc3..551ae2ec821c 100644 --- a/clang/lib/Driver/ToolChains/OHOS.cpp +++ b/clang/lib/Driver/ToolChains/OHOS.cpp @@ -297,11 +297,11 @@ std::string OHOS::computeSysRoot() const { !getDriver().SysRoot.empty() ? getDriver().SysRoot : makePath({getDriver().getInstalledDir(), "..", "..", "sysroot"}); - if (!llvm::sys::fs::exists(SysRoot)) + if (!getVFS().exists(SysRoot)) return std::string(); std::string ArchRoot = makePath({SysRoot, getMultiarchTriple(getTriple())}); - return llvm::sys::fs::exists(ArchRoot) ? ArchRoot : SysRoot; + return getVFS().exists(ArchRoot) ? ArchRoot : SysRoot; } ToolChain::path_list OHOS::getRuntimePaths() const { diff --git a/clang/unittests/Driver/CMakeLists.txt b/clang/unittests/Driver/CMakeLists.txt index aced8f4c467e..4b0228953e81 100644 --- a/clang/unittests/Driver/CMakeLists.txt +++ b/clang/unittests/Driver/CMakeLists.txt @@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS add_clang_unittest(ClangDriverTests DistroTest.cpp ToolChainTest.cpp + OHOSTCTest.cpp ModuleCacheTest.cpp MultilibTest.cpp SanitizerArgsTest.cpp diff --git a/clang/unittests/Driver/OHOSTCTest.cpp b/clang/unittests/Driver/OHOSTCTest.cpp new file mode 100644 index 000000000000..618dff9def9c --- /dev/null +++ b/clang/unittests/Driver/OHOSTCTest.cpp @@ -0,0 +1,78 @@ +//===- unittests/Driver/OHOSTCTest.cpp --- OHOS ToolChain tests -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Unit tests for the OHOS-specific ToolChain. +// +//===----------------------------------------------------------------------===// + +#include "clang/Driver/ToolChain.h" +#include "clang/Basic/DiagnosticIDs.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Driver/Compilation.h" +#include "clang/Driver/Driver.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/VirtualFileSystem.h" +#include "gmock/gmock.h" +#include + +using namespace testing; + +using namespace clang; +using namespace clang::driver; + +namespace { + +std::string rectifyPathSeparators(const char *S) { + std::string Str(S); + std::replace(Str.begin(), Str.end(), '\\', '/'); + return Str; +} + +TEST(OHOSTCTest, VFSSysroot) { + IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); + + IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); + struct TestDiagnosticConsumer : public DiagnosticConsumer {}; + IntrusiveRefCntPtr InMemoryFileSystem( + new llvm::vfs::InMemoryFileSystem); + + InMemoryFileSystem->addFile("/ohos-sysroot", 0, + llvm::MemoryBuffer::getMemBuffer("")); + InMemoryFileSystem->addFile("/include/libcxx-ohos/include/c++/v1", 0, + llvm::MemoryBuffer::getMemBuffer("")); + + DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); + Driver TheDriver("/bin/clang", "arm-linux-liteos", Diags, + "clang LLVM compiler", InMemoryFileSystem); + std::unique_ptr C(TheDriver.BuildCompilation( + {"-fsyntax-only", "--sysroot=/ohos-sysroot", "foo.cpp"})); + ASSERT_TRUE(C); + + // Check that sysroot search uses provided VFS + EXPECT_EQ(C->getDefaultToolChain().computeSysRoot(), "/ohos-sysroot"); + + // Check additional include paths + llvm::opt::ArgStringList Paths; + C->getDefaultToolChain().AddClangCXXStdlibIncludeArgs(C->getArgs(), Paths); + SmallVector Converted; + if (is_style_windows(llvm::sys::path::Style::native)) + std::transform(Paths.begin(), Paths.end(), std::back_inserter(Converted), + rectifyPathSeparators); + else + Converted = SmallVector(Paths.begin(), Paths.end()); + + ASSERT_THAT(Converted, Contains(llvm::StringRef( + "/bin/../include/libcxx-ohos/include/c++/v1"))); +} + +} // end anonymous namespace. -- Gitee