2015-10-07 23:48:01 +08:00
|
|
|
//===- unittests/Driver/ToolChainTest.cpp --- ToolChain tests -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-10-07 23:48:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Unit tests for ToolChains.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/ToolChain.h"
|
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
2017-08-29 13:22:26 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2018-10-10 21:27:25 +08:00
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
2015-10-07 23:48:01 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(ToolChainTest, VFSGCCInstallation) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
|
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2015-10-13 23:19:32 +08:00
|
|
|
Driver TheDriver("/bin/clang", "arm-linux-gnueabihf", Diags,
|
2015-10-07 23:48:01 +08:00
|
|
|
InMemoryFileSystem);
|
|
|
|
|
|
|
|
const char *EmptyFiles[] = {
|
|
|
|
"foo.cpp",
|
2015-10-13 23:19:32 +08:00
|
|
|
"/bin/clang",
|
2015-10-07 23:48:01 +08:00
|
|
|
"/usr/lib/gcc/arm-linux-gnueabi/4.6.1/crtbegin.o",
|
|
|
|
"/usr/lib/gcc/arm-linux-gnueabi/4.6.1/crtend.o",
|
|
|
|
"/usr/lib/gcc/arm-linux-gnueabihf/4.6.3/crtbegin.o",
|
|
|
|
"/usr/lib/gcc/arm-linux-gnueabihf/4.6.3/crtend.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabi/crt1.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabi/crti.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabi/crtn.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabihf/crt1.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabihf/crti.o",
|
|
|
|
"/usr/lib/arm-linux-gnueabihf/crtn.o",
|
|
|
|
"/usr/include/arm-linux-gnueabi/.keep",
|
|
|
|
"/usr/include/arm-linux-gnueabihf/.keep",
|
|
|
|
"/lib/arm-linux-gnueabi/.keep",
|
|
|
|
"/lib/arm-linux-gnueabihf/.keep"};
|
|
|
|
|
|
|
|
for (const char *Path : EmptyFiles)
|
|
|
|
InMemoryFileSystem->addFile(Path, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer("\n"));
|
|
|
|
|
2015-10-28 06:20:26 +08:00
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(
|
2019-09-28 20:21:06 +08:00
|
|
|
{"-fsyntax-only", "--gcc-toolchain=", "--sysroot=", "foo.cpp"}));
|
2017-05-24 22:57:17 +08:00
|
|
|
EXPECT_TRUE(C);
|
2015-10-07 23:48:01 +08:00
|
|
|
|
|
|
|
std::string S;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
C->getDefaultToolChain().printVerboseInfo(OS);
|
|
|
|
}
|
2018-04-28 03:11:14 +08:00
|
|
|
#if _WIN32
|
2015-10-10 00:48:52 +08:00
|
|
|
std::replace(S.begin(), S.end(), '\\', '/');
|
|
|
|
#endif
|
2015-10-09 21:03:18 +08:00
|
|
|
EXPECT_EQ(
|
|
|
|
"Found candidate GCC installation: "
|
|
|
|
"/usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n"
|
|
|
|
"Selected GCC installation: /usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n"
|
|
|
|
"Candidate multilib: .;@m32\n"
|
|
|
|
"Selected multilib: .;@m32\n",
|
|
|
|
S);
|
2015-10-07 23:48:01 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 23:19:32 +08:00
|
|
|
TEST(ToolChainTest, VFSGCCInstallationRelativeDir) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
|
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2015-10-13 23:19:32 +08:00
|
|
|
Driver TheDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags,
|
|
|
|
InMemoryFileSystem);
|
|
|
|
|
|
|
|
const char *EmptyFiles[] = {
|
|
|
|
"foo.cpp", "/home/test/lib/gcc/arm-linux-gnueabi/4.6.1/crtbegin.o",
|
|
|
|
"/home/test/include/arm-linux-gnueabi/.keep"};
|
|
|
|
|
|
|
|
for (const char *Path : EmptyFiles)
|
|
|
|
InMemoryFileSystem->addFile(Path, 0,
|
|
|
|
llvm::MemoryBuffer::getMemBuffer("\n"));
|
|
|
|
|
2015-10-28 06:20:26 +08:00
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(
|
|
|
|
{"-fsyntax-only", "--gcc-toolchain=", "foo.cpp"}));
|
2017-05-24 22:57:17 +08:00
|
|
|
EXPECT_TRUE(C);
|
2015-10-13 23:19:32 +08:00
|
|
|
|
|
|
|
std::string S;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
C->getDefaultToolChain().printVerboseInfo(OS);
|
|
|
|
}
|
2018-04-28 03:11:14 +08:00
|
|
|
#if _WIN32
|
2015-10-13 23:19:32 +08:00
|
|
|
std::replace(S.begin(), S.end(), '\\', '/');
|
|
|
|
#endif
|
|
|
|
EXPECT_EQ("Found candidate GCC installation: "
|
[VirtualFileSystem] InMemoryFileSystem::status: Return a Status with the requested name
Summary:
InMemoryFileSystem::status behaves differently than
RealFileSystem::status. The Name contained in the Status returned by
RealFileSystem::status will be the path as requested by the caller,
whereas InMemoryFileSystem::status returns the normalized path.
For example, when requested the status for "../src/first.h",
RealFileSystem returns a Status with "../src/first.h" as the Name.
InMemoryFileSystem returns "/absolute/path/to/src/first.h".
The reason for this change is that I want to make a unit test in the
clangd testsuite (where we use an InMemoryFileSystem) to reproduce a
bug I get with the clangd program (where a RealFileSystem is used).
This difference in behavior "hides" the bug in the unit test version.
An indirect impact of this change is that a -Wnonportable-include-path
warning is now emitted in test PCH/case-insensitive-include.c. This is
because the real path of the included file (with the wrong case) was not
available previously, whereas it is now.
Reviewers: malaperle, ilya-biryukov, bkramer
Reviewed By: ilya-biryukov
Subscribers: eric_niebler, malaperle, omtcyfz, hokein, bkramer, ilya-biryukov, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D48903
llvm-svn: 339063
2018-08-07 05:48:20 +08:00
|
|
|
"/home/test/bin/../lib/gcc/arm-linux-gnueabi/4.6.1\n"
|
2015-10-13 23:19:32 +08:00
|
|
|
"Selected GCC installation: "
|
|
|
|
"/home/test/bin/../lib/gcc/arm-linux-gnueabi/4.6.1\n"
|
|
|
|
"Candidate multilib: .;@m32\n"
|
|
|
|
"Selected multilib: .;@m32\n",
|
|
|
|
S);
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:47:52 +08:00
|
|
|
TEST(ToolChainTest, DefaultDriverMode) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
|
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
|
|
|
new llvm::vfs::InMemoryFileSystem);
|
2016-08-13 01:47:52 +08:00
|
|
|
|
|
|
|
Driver CCDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags,
|
|
|
|
InMemoryFileSystem);
|
2017-05-24 22:57:17 +08:00
|
|
|
CCDriver.setCheckInputsExist(false);
|
2016-08-13 01:47:52 +08:00
|
|
|
Driver CXXDriver("/home/test/bin/clang++", "arm-linux-gnueabi", Diags,
|
|
|
|
InMemoryFileSystem);
|
2017-05-24 22:57:17 +08:00
|
|
|
CXXDriver.setCheckInputsExist(false);
|
2016-08-13 01:47:52 +08:00
|
|
|
Driver CLDriver("/home/test/bin/clang-cl", "arm-linux-gnueabi", Diags,
|
|
|
|
InMemoryFileSystem);
|
2017-05-24 22:57:17 +08:00
|
|
|
CLDriver.setCheckInputsExist(false);
|
|
|
|
|
|
|
|
std::unique_ptr<Compilation> CC(CCDriver.BuildCompilation(
|
|
|
|
{ "/home/test/bin/clang", "foo.cpp"}));
|
|
|
|
std::unique_ptr<Compilation> CXX(CXXDriver.BuildCompilation(
|
|
|
|
{ "/home/test/bin/clang++", "foo.cpp"}));
|
|
|
|
std::unique_ptr<Compilation> CL(CLDriver.BuildCompilation(
|
|
|
|
{ "/home/test/bin/clang-cl", "foo.cpp"}));
|
|
|
|
|
|
|
|
EXPECT_TRUE(CC);
|
|
|
|
EXPECT_TRUE(CXX);
|
|
|
|
EXPECT_TRUE(CL);
|
2016-08-13 01:47:52 +08:00
|
|
|
EXPECT_TRUE(CCDriver.CCCIsCC());
|
|
|
|
EXPECT_TRUE(CXXDriver.CCCIsCXX());
|
|
|
|
EXPECT_TRUE(CLDriver.IsCLMode());
|
|
|
|
}
|
2017-06-30 21:21:27 +08:00
|
|
|
TEST(ToolChainTest, InvalidArgument) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
struct TestDiagnosticConsumer : public DiagnosticConsumer {};
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
|
|
|
|
Driver TheDriver("/bin/clang", "arm-linux-gnueabihf", Diags);
|
|
|
|
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(
|
|
|
|
{"-fsyntax-only", "-fan-unknown-option", "foo.cpp"}));
|
|
|
|
EXPECT_TRUE(C);
|
|
|
|
EXPECT_TRUE(C->containsError());
|
|
|
|
}
|
2016-08-13 01:47:52 +08:00
|
|
|
|
2017-08-29 13:22:26 +08:00
|
|
|
TEST(ToolChainTest, ParsedClangName) {
|
|
|
|
ParsedClangName Empty;
|
|
|
|
EXPECT_TRUE(Empty.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Empty.ModeSuffix.empty());
|
|
|
|
EXPECT_TRUE(Empty.DriverMode == nullptr);
|
|
|
|
EXPECT_FALSE(Empty.TargetIsValid);
|
|
|
|
|
|
|
|
ParsedClangName DriverOnly("clang", nullptr);
|
|
|
|
EXPECT_TRUE(DriverOnly.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(DriverOnly.ModeSuffix == "clang");
|
|
|
|
EXPECT_TRUE(DriverOnly.DriverMode == nullptr);
|
|
|
|
EXPECT_FALSE(DriverOnly.TargetIsValid);
|
|
|
|
|
|
|
|
ParsedClangName DriverOnly2("clang++", "--driver-mode=g++");
|
|
|
|
EXPECT_TRUE(DriverOnly2.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(DriverOnly2.ModeSuffix == "clang++");
|
|
|
|
EXPECT_STREQ(DriverOnly2.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_FALSE(DriverOnly2.TargetIsValid);
|
|
|
|
|
|
|
|
ParsedClangName TargetAndMode("i386", "clang-g++", "--driver-mode=g++", true);
|
|
|
|
EXPECT_TRUE(TargetAndMode.TargetPrefix == "i386");
|
|
|
|
EXPECT_TRUE(TargetAndMode.ModeSuffix == "clang-g++");
|
|
|
|
EXPECT_STREQ(TargetAndMode.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_TRUE(TargetAndMode.TargetIsValid);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ToolChainTest, GetTargetAndMode) {
|
|
|
|
llvm::InitializeAllTargets();
|
|
|
|
std::string IgnoredError;
|
|
|
|
if (!llvm::TargetRegistry::lookupTarget("x86_64", IgnoredError))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ParsedClangName Res = ToolChain::getTargetAndModeFromProgramName("clang");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang");
|
|
|
|
EXPECT_TRUE(Res.DriverMode == nullptr);
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("clang++");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("clang++6.0");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("clang++-release");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("x86_64-clang++");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix == "x86_64");
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_TRUE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName(
|
|
|
|
"x86_64-linux-gnu-clang-c++");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix == "x86_64-linux-gnu");
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang-c++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_TRUE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName(
|
|
|
|
"x86_64-linux-gnu-clang-c++-tot");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix == "x86_64-linux-gnu");
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang-c++");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=g++");
|
|
|
|
EXPECT_TRUE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("qqq");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix.empty());
|
|
|
|
EXPECT_TRUE(Res.DriverMode == nullptr);
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("x86_64-qqq");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix.empty());
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix.empty());
|
|
|
|
EXPECT_TRUE(Res.DriverMode == nullptr);
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
|
|
|
|
Res = ToolChain::getTargetAndModeFromProgramName("qqq-clang-cl");
|
|
|
|
EXPECT_TRUE(Res.TargetPrefix == "qqq");
|
|
|
|
EXPECT_TRUE(Res.ModeSuffix == "clang-cl");
|
|
|
|
EXPECT_STREQ(Res.DriverMode, "--driver-mode=cl");
|
|
|
|
EXPECT_FALSE(Res.TargetIsValid);
|
|
|
|
}
|
2017-04-19 01:34:46 +08:00
|
|
|
} // end anonymous namespace.
|