From 5cf49ef1d03a9767b32639ce6f0638b9662bf65c Mon Sep 17 00:00:00 2001 From: luofeng14 Date: Mon, 29 Apr 2024 14:08:46 +0800 Subject: [PATCH 1/2] set default prefix for search path when prefix is empty --- clang/lib/Frontend/CompilerInvocation.cpp | 31 +++++++++++++++++++++++ clang/test/Driver/prefix.c | 10 ++++++++ 2 files changed, 41 insertions(+) create mode 100644 clang/test/Driver/prefix.c diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 1fba91bed041..c844452dee36 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -3127,6 +3127,36 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args, IsIndexHeaderMap = false; } +#ifdef BUILD_FOR_OPENEULER + // Add -iprefix/-iwithprefix/-iwithprefixbefore options. + StringRef Prefix = ""; // FIXME: This isn't the correct default prefix. + if (Args.hasFlag(options::OPT_fgcc_compatible, options::OPT_fno_gcc_compatible, false)) { + Prefix = Opts.ResourceDir; // default prefix. + } + for (const auto *A : + Args.filtered(OPT_iprefix, OPT_iwithprefix, OPT_iwithprefixbefore)) { + llvm::SmallString<128> searchPath(Prefix.str()); + if (Args.hasFlag(options::OPT_fgcc_compatible, options::OPT_fno_gcc_compatible, false)) { + llvm::sys::path::append(searchPath, A->getValue()); + } + if (A->getOption().matches(OPT_iprefix)) + Prefix = A->getValue(); + else if (A->getOption().matches(OPT_iwithprefix)) { + if (Args.hasFlag(options::OPT_fgcc_compatible, options::OPT_fno_gcc_compatible, false)) { + Opts.AddPath(searchPath, frontend::After, false, true); + } else { + Opts.AddPath(Prefix.str() + A->getValue(), frontend::After, false, true); + } + } + else { + if (Args.hasFlag(options::OPT_fgcc_compatible, options::OPT_fno_gcc_compatible, false)) { + Opts.AddPath(searchPath, frontend::Angled, false, true); + } else { + Opts.AddPath(Prefix.str() + A->getValue(), frontend::Angled, false, true); + } + } + } +#else // Add -iprefix/-iwithprefix/-iwithprefixbefore options. StringRef Prefix = ""; // FIXME: This isn't the correct default prefix. for (const auto *A : @@ -3138,6 +3168,7 @@ static bool ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args, else Opts.AddPath(Prefix.str() + A->getValue(), frontend::Angled, false, true); } +#endif for (const auto *A : Args.filtered(OPT_idirafter)) Opts.AddPath(A->getValue(), frontend::After, false, true); diff --git a/clang/test/Driver/prefix.c b/clang/test/Driver/prefix.c new file mode 100644 index 000000000000..0590beef26d2 --- /dev/null +++ b/clang/test/Driver/prefix.c @@ -0,0 +1,10 @@ +// REQUIRES: build_for_openeuler + +// RUN: %clang -v -fgcc-compatible -iwithprefix include %s +// RUN: %clang -v -fgcc-compatible -nostdinc -iwithprefix include %s +// RUN: %clang -v -fgcc-compatible %s + +#include +int main(void) { +return 0; +} -- Gitee From 39ad0c19af40051228ead821f81c9b035d95cc46 Mon Sep 17 00:00:00 2001 From: luofeng14 Date: Thu, 19 Dec 2024 13:38:56 +0000 Subject: [PATCH 2/2] add debug. Signed-off-by: luofeng14 --- debug | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 debug diff --git a/debug b/debug new file mode 100644 index 000000000000..11a6fadbeca8 --- /dev/null +++ b/debug @@ -0,0 +1,20 @@ + + // Add -iprefix/-iwithprefix/-iwithprefixbefore options. + StringRef Prefix = ""; // FIXME: This isn't the correct default prefix. + bool iprefixEmpty = true; + for (const auto *A : + Args.filtered(OPT_iprefix, OPT_iwithprefix, OPT_iwithprefixbefore)) { + if (A->getOption().matches(OPT_iprefix)) { + Prefix = A->getValue(); + iprefixEmpty = false; + } else if (A->getOption().matches(OPT_iwithprefix)) { + if (Args.hasFlag(options::OPT_fgcc_compatible, options::OPT_fno_gcc_compatible, false) && iprefixEmpty) { + SmallString<256> path; + llvm::sys::path::append(path, Opts.ResourceDir, A->getValue()); + Opts.AddPath(path, frontend::Angled, false, true); + } else { + Opts.AddPath(Prefix.str() + A->getValue(), frontend::After, false, true); + } + } else + Opts.AddPath(Prefix.str() + A->getValue(), frontend::Angled, false, true); + } \ No newline at end of file -- Gitee