Skip to content

Commit e0ba172

Browse files
committed
[Clang] [Driver] Canoncalise -internal-isystem include paths
1 parent c4fc358 commit e0ba172

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

clang/include/clang/Driver/ToolChain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ class ToolChain {
228228
static void addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
229229
llvm::opt::ArgStringList &CC1Args,
230230
const Twine &Path);
231+
232+
public:
231233
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
232234
llvm::opt::ArgStringList &CC1Args,
233235
const Twine &Path);
236+
237+
protected:
234238
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
235239
llvm::opt::ArgStringList &CC1Args,
236240
const Twine &Path);

clang/lib/Driver/ToolChain.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,19 +1370,48 @@ ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{
13701370
return *cxxStdlibType;
13711371
}
13721372

1373+
1374+
static void ResolveAndAddSystemIncludePath(const ArgList &DriverArgs,
1375+
ArgStringList &CC1Args,
1376+
const Twine &Path) {
1377+
bool Canonicalize =
1378+
DriverArgs.hasFlag(options::OPT_canonical_prefixes,
1379+
options::OPT_no_canonical_prefixes, true);
1380+
1381+
if (!Canonicalize) {
1382+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1383+
return;
1384+
}
1385+
1386+
// We canonicalise system include paths that were added automatically if
1387+
// that yields a shorter path since those can end up quite long otherwise.
1388+
//
1389+
// While we would ideally prefer to use FileManager for this, there doesn't
1390+
// seem to be a way to obtain one in here, so we just resolve these via the
1391+
// real file system; most system libraries will hopefully correspond to
1392+
// actual files.
1393+
IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem();
1394+
SmallString<256> Canonical, PathStorage;
1395+
StringRef SimplifiedPath = Path.toStringRef(PathStorage);
1396+
if (!VFS->getRealPath(SimplifiedPath, Canonical) &&
1397+
Canonical.size() < SimplifiedPath.size())
1398+
SimplifiedPath = Canonical;
1399+
CC1Args.push_back(DriverArgs.MakeArgString(SimplifiedPath));
1400+
}
1401+
13731402
/// Utility function to add a system framework directory to CC1 arguments.
13741403
void ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
13751404
llvm::opt::ArgStringList &CC1Args,
13761405
const Twine &Path) {
13771406
CC1Args.push_back("-internal-iframework");
1378-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1407+
ResolveAndAddSystemIncludePath(DriverArgs, CC1Args, Path);
13791408
}
13801409

13811410
/// Utility function to add a system include directory to CC1 arguments.
13821411
void ToolChain::addSystemInclude(const ArgList &DriverArgs,
13831412
ArgStringList &CC1Args, const Twine &Path) {
13841413
CC1Args.push_back("-internal-isystem");
1385-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1414+
ResolveAndAddSystemIncludePath(DriverArgs, CC1Args, Path);
13861415
}
13871416

13881417
/// Utility function to add a system include directory with extern "C"
@@ -1397,7 +1426,7 @@ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs,
13971426
ArgStringList &CC1Args,
13981427
const Twine &Path) {
13991428
CC1Args.push_back("-internal-externc-isystem");
1400-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1429+
ResolveAndAddSystemIncludePath(DriverArgs, CC1Args, Path);
14011430
}
14021431

14031432
void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
@@ -1411,20 +1440,16 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
14111440
void ToolChain::addSystemFrameworkIncludes(const ArgList &DriverArgs,
14121441
ArgStringList &CC1Args,
14131442
ArrayRef<StringRef> Paths) {
1414-
for (const auto &Path : Paths) {
1415-
CC1Args.push_back("-internal-iframework");
1416-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1417-
}
1443+
for (const auto &Path : Paths)
1444+
addSystemFrameworkInclude(DriverArgs, CC1Args, Path);
14181445
}
14191446

14201447
/// Utility function to add a list of system include directories to CC1.
14211448
void ToolChain::addSystemIncludes(const ArgList &DriverArgs,
14221449
ArgStringList &CC1Args,
14231450
ArrayRef<StringRef> Paths) {
1424-
for (const auto &Path : Paths) {
1425-
CC1Args.push_back("-internal-isystem");
1426-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1427-
}
1451+
for (const auto &Path : Paths)
1452+
addSystemInclude(DriverArgs, CC1Args, Path);
14281453
}
14291454

14301455
std::string ToolChain::concat(StringRef Path, const Twine &A, const Twine &B,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
946946
SmallString<128> P(D.ResourceDir);
947947
llvm::sys::path::append(P, "include");
948948
llvm::sys::path::append(P, "openmp_wrappers");
949-
CmdArgs.push_back("-internal-isystem");
950-
CmdArgs.push_back(Args.MakeArgString(P));
949+
getToolChain().addSystemInclude(Args, CmdArgs, P);
951950
}
952951

953952
CmdArgs.push_back("-include");
@@ -959,7 +958,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
959958
// standard library headers and other headers.
960959
SmallString<128> P(D.ResourceDir);
961960
llvm::sys::path::append(P, "include", "llvm_offload_wrappers");
962-
CmdArgs.append({"-internal-isystem", Args.MakeArgString(P), "-include"});
961+
getToolChain().addSystemInclude(Args, CmdArgs, P);
962+
CmdArgs.push_back("-include");
963963
if (JA.isDeviceOffloading(Action::OFK_OpenMP))
964964
CmdArgs.push_back("__llvm_offload_device.h");
965965
else
@@ -1132,16 +1132,14 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11321132
SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
11331133
llvm::sys::path::append(P, "include");
11341134
llvm::sys::path::append(P, getToolChain().getTripleString());
1135-
CmdArgs.push_back("-internal-isystem");
1136-
CmdArgs.push_back(Args.MakeArgString(P));
1135+
getToolChain().addSystemInclude(Args, CmdArgs, P);
11371136
} else if (C.getActiveOffloadKinds() == Action::OFK_OpenMP) {
11381137
// TODO: CUDA / HIP include their own headers for some common functions
11391138
// implemented here. We'll need to clean those up so they do not conflict.
11401139
SmallString<128> P(D.ResourceDir);
11411140
llvm::sys::path::append(P, "include");
11421141
llvm::sys::path::append(P, "llvm_libc_wrappers");
1143-
CmdArgs.push_back("-internal-isystem");
1144-
CmdArgs.push_back(Args.MakeArgString(P));
1142+
getToolChain().addSystemInclude(Args, CmdArgs, P);
11451143
}
11461144
}
11471145

0 commit comments

Comments
 (0)