Skip to content

Commit 50a3368

Browse files
authored
[Clang] Take libstdc++ into account during GCC detection (#145056)
The Generic_GCC::GCCInstallationDetector class picks the GCC installation directory with the largest version number. Since the location of the libstdc++ include directories is tied to the GCC version, this can break C++ compilation if the libstdc++ headers for this particular GCC version are not available. Linux distributions tend to package the libstdc++ headers separately from GCC. This frequently leads to situations in which a newer version of GCC gets installed as a dependency of another package without installing the corresponding libstdc++ package. Clang then fails to compile C++ code because it cannot find the libstdc++ headers. Since libstdc++ headers are in fact installed on the system, the GCC installation continues to work, the user may not be aware of the details of the GCC detection, and the compiler does not recognize the situation and emit a warning, this behavior can be hard to understand - as witnessed by many related bug reports over the years. The goal of this work is to change the GCC detection to prefer GCC installations that contain libstdc++ include directories over those which do not. This should happen regardless of the input language since picking different GCC installations for a build that mixes C and C++ might lead to incompatibilities. Any change to the GCC installation detection will probably have a negative impact on some users. For instance, for a C user who relies on using the GCC installation with the largest version number, it might become necessary to use the --gcc-install-dir option to ensure that this GCC version is selected. This seems like an acceptable trade-off given that the situation for users who do not have any special demands on the particular GCC installation directory would be improved significantly. This patch does not yet change the automatic GCC installation directory choice. Instead, it does introduce a warning that informs the user about the future change if the chosen GCC installation directory differs from the one that would be chosen if the libstdc++ headers are taken into account. See also this related Discourse discussion: https://discourse.llvm.org/t/rfc-take-libstdc-into-account-during-gcc-detection/86992.
1 parent 355b747 commit 50a3368

File tree

37 files changed

+320
-108
lines changed

37 files changed

+320
-108
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ latest release, please see the `Clang Web Site <https://clang.llvm.org>`_ or the
3737
Potentially Breaking Changes
3838
============================
3939

40+
- Clang will now emit a warning if the auto-detected GCC installation
41+
directory (i.e. the one with the largest version number) does not
42+
contain libstdc++ include directories although a "complete" GCC
43+
installation directory containing the include directories is
44+
available. It is planned to change the auto-detection to prefer the
45+
"complete" directory in the future. The warning will disappear if
46+
the libstdc++ include directories are either installed or removed
47+
for all GCC installation directories considered by the
48+
auto-detection; see the output of ``clang -v`` for a list of those
49+
directories. If the GCC installations cannot be modified and
50+
maintaining the current choice of the auto-detection is desired, the
51+
GCC installation directory can be selected explicitly using the
52+
``--gcc-install-dir`` command line argument. This will silence the
53+
warning. It can also be disabled using the
54+
``-Wno-gcc-install-dir-libstdcxx`` command line flag.
55+
4056
C/C++ Language Potentially Breaking Changes
4157
-------------------------------------------
4258

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,4 +885,9 @@ def warn_drv_openacc_without_cir
885885
: Warning<"OpenACC directives will result in no runtime behavior; use "
886886
"-fclangir to enable runtime effect">,
887887
InGroup<SourceUsesOpenACC>;
888+
889+
def warn_drv_gcc_install_dir_libstdcxx : Warning<
890+
"future releases of the clang compiler will prefer GCC installations "
891+
"containing libstdc++ include directories; '%0' would be chosen over '%1'">,
892+
InGroup<DiagGroup<"gcc-install-dir-libstdcxx">>;
888893
}

clang/include/clang/Driver/ToolChain.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ class ToolChain {
224224
static void addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
225225
llvm::opt::ArgStringList &CC1Args,
226226
const Twine &Path);
227-
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
228-
llvm::opt::ArgStringList &CC1Args,
229-
const Twine &Path);
230227
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs,
231228
llvm::opt::ArgStringList &CC1Args,
232229
const Twine &Path);
@@ -246,6 +243,9 @@ class ToolChain {
246243
///@}
247244

248245
public:
246+
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs,
247+
llvm::opt::ArgStringList &CC1Args,
248+
const Twine &Path);
249249
virtual ~ToolChain();
250250

251251
// Accessors

clang/lib/Driver/ToolChain.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,13 +1409,6 @@ void ToolChain::addSystemFrameworkInclude(const llvm::opt::ArgList &DriverArgs,
14091409
CC1Args.push_back(DriverArgs.MakeArgString(Path));
14101410
}
14111411

1412-
/// Utility function to add a system include directory to CC1 arguments.
1413-
void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1414-
ArgStringList &CC1Args, const Twine &Path) {
1415-
CC1Args.push_back("-internal-isystem");
1416-
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1417-
}
1418-
14191412
/// Utility function to add a system include directory with extern "C"
14201413
/// semantics to CC1 arguments.
14211414
///
@@ -1438,6 +1431,14 @@ void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs,
14381431
addExternCSystemInclude(DriverArgs, CC1Args, Path);
14391432
}
14401433

1434+
/// Utility function to add a system include directory to CC1 arguments.
1435+
/*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs,
1436+
ArgStringList &CC1Args,
1437+
const Twine &Path) {
1438+
CC1Args.push_back("-internal-isystem");
1439+
CC1Args.push_back(DriverArgs.MakeArgString(Path));
1440+
}
1441+
14411442
/// Utility function to add a list of system framework directories to CC1.
14421443
void ToolChain::addSystemFrameworkIncludes(const ArgList &DriverArgs,
14431444
ArgStringList &CC1Args,

0 commit comments

Comments
 (0)