[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
This revision was automatically updated to reflect the committed changes. Closed by commit rC342021: [Driver] Search LibraryPaths when handling -print-file-name (authored by phosek, committed by ). Changed prior to commit: https://reviews.llvm.org/D51573?vs=163636=165008#toc Repository: rC Clang https://reviews.llvm.org/D51573 Files: lib/Driver/Driver.cpp test/Driver/linux-per-target-runtime-dir.c Index: lib/Driver/Driver.cpp === --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -4152,16 +4152,24 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + // Seach for Name in a list of paths. + auto SearchPaths = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const auto : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return {P.str()}; +} +return None; + }; + + if (auto P = SearchPaths(PrefixDirs)) +return *P; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4173,14 +4181,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (auto P = SearchPaths(TC.getLibraryPaths())) +return *P; + + if (auto P = SearchPaths(TC.getFilePaths())) +return *P; return Name; } Index: test/Driver/linux-per-target-runtime-dir.c === --- test/Driver/linux-per-target-runtime-dir.c +++ test/Driver/linux-per-target-runtime-dir.c @@ -19,3 +19,9 @@ // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ // RUN: | FileCheck --check-prefix=CHECK-CLANGRT-X8664 %s // CHECK-CLANGRT-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a + +// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \ +// RUN: --target=x86_64-linux-gnu \ +// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ +// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-X8664 %s +// CHECK-FILE-NAME-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a Index: lib/Driver/Driver.cpp === --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -4152,16 +4152,24 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + // Seach for Name in a list of paths. + auto SearchPaths = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const auto : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return {P.str()}; +} +return None; + }; + + if (auto P = SearchPaths(PrefixDirs)) +return *P; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4173,14 +4181,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (auto P = SearchPaths(TC.getLibraryPaths())) +return *P; + + if (auto P = SearchPaths(TC.getFilePaths())) +return *P; return Name; } Index: test/Driver/linux-per-target-runtime-dir.c
[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
phosek added inline comments. Comment at: clang/lib/Driver/Driver.cpp:4187 SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); mcgrathr wrote: > You could fold these cases into the lambda too by having it take a bool > UseSysRoot and passing `{ResourceDir}, false`. > Then the main body is a uniform and concise list of paths to check. I considered that but `llvm::SmallVector` doesn't support initializer list so this ends up looking not so great. Repository: rC Clang https://reviews.llvm.org/D51573 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
mcgrathr accepted this revision. mcgrathr added a comment. This revision is now accepted and ready to land. The log message could give concrete examples. Comment at: clang/lib/Driver/Driver.cpp:4169 std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { A one-line comment before the lambda would help: `// Check for Name in Path.` (rename the param to be clearer) Comment at: clang/lib/Driver/Driver.cpp:4173 +// attempting to use this prefix when looking for file paths. +for (const std::string : P) { + if (Dir.empty()) I'd always use `const auto&` in a range-for like this, but I guess this was just moved around and maybe LLVM style frowns on auto? Comment at: clang/lib/Driver/Driver.cpp:4179 + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} Can this be `return {P.str()};`? Comment at: clang/lib/Driver/Driver.cpp:4184 + + if (Optional D = FindPath(PrefixDirs)) +return *D; I'd always use auto in these situations (here and below). Comment at: clang/lib/Driver/Driver.cpp:4187 SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); You could fold these cases into the lambda too by having it take a bool UseSysRoot and passing `{ResourceDir}, false`. Then the main body is a uniform and concise list of paths to check. Repository: rC Clang https://reviews.llvm.org/D51573 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
phosek updated this revision to Diff 163633. Repository: rC Clang https://reviews.llvm.org/D51573 Files: clang/lib/Driver/Driver.cpp clang/test/Driver/linux-per-target-runtime-dir.c Index: clang/test/Driver/linux-per-target-runtime-dir.c === --- clang/test/Driver/linux-per-target-runtime-dir.c +++ clang/test/Driver/linux-per-target-runtime-dir.c @@ -19,3 +19,9 @@ // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ // RUN: | FileCheck --check-prefix=CHECK-CLANGRT-X8664 %s // CHECK-CLANGRT-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a + +// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \ +// RUN: --target=x86_64-linux-gnu \ +// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ +// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-X8664 %s +// CHECK-FILE-NAME-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4187,14 +4194,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(TC.getLibraryPaths())) +return *D; + + if (Optional D = FindPath(TC.getFilePaths())) +return *D; return Name; } Index: clang/test/Driver/linux-per-target-runtime-dir.c === --- clang/test/Driver/linux-per-target-runtime-dir.c +++ clang/test/Driver/linux-per-target-runtime-dir.c @@ -19,3 +19,9 @@ // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ // RUN: | FileCheck --check-prefix=CHECK-CLANGRT-X8664 %s // CHECK-CLANGRT-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a + +// RUN: %clang -rtlib=compiler-rt -print-file-name=libclang_rt.builtins.a 2>&1 \ +// RUN: --target=x86_64-linux-gnu \ +// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ +// RUN: | FileCheck --check-prefix=CHECK-FILE-NAME-X8664 %s +// CHECK-FILE-NAME-X8664: x86_64-linux-gnu{{/|\\}}lib{{/|\\}}libclang_rt.builtins.a Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir);
[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
phosek updated this revision to Diff 163630. Repository: rC Clang https://reviews.llvm.org/D51573 Files: clang/lib/Driver/Driver.cpp Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4187,14 +4194,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(TC.getLibraryPaths())) +return *D; + + if (Optional D = FindPath(TC.getFilePaths())) +return *D; return Name; } Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { - // Respect a limited subset of the '-Bprefix' functionality in GCC by - // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +// Respect a limited subset of the '-Bprefix' functionality in GCC by +// attempting to use this prefix when looking for file paths. +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4187,14 +4194,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(TC.getLibraryPaths())) +return *D; + + if (Optional D = FindPath(TC.getFilePaths())) +return *D; return Name; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D51573: [Driver] Search LibraryPaths when handling -print-file-name
phosek created this revision. phosek added a reviewer: beanz. Herald added a subscriber: cfe-commits. This is necessary to handle the multiarch runtime directories. Repository: rC Clang https://reviews.llvm.org/D51573 Files: clang/lib/Driver/Driver.cpp Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + // Respect a limited subset of the '-Bprefix' functionality in GCC by // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4187,14 +4194,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(TC.getLibraryPaths())) +return *D; + + if (Optional D = FindPath(TC.getFilePaths())) +return *D; return Name; } Index: clang/lib/Driver/Driver.cpp === --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -4166,16 +4166,23 @@ } std::string Driver::GetFilePath(StringRef Name, const ToolChain ) const { + auto FindPath = [&](const llvm::SmallVectorImpl ) + -> llvm::Optional { +for (const std::string : P) { + if (Dir.empty()) +continue; + SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); + llvm::sys::path::append(P, Name); + if (llvm::sys::fs::exists(Twine(P))) +return std::string(P.str()); +} +return None; + }; + // Respect a limited subset of the '-Bprefix' functionality in GCC by // attempting to use this prefix when looking for file paths. - for (const std::string : PrefixDirs) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(PrefixDirs)) +return *D; SmallString<128> R(ResourceDir); llvm::sys::path::append(R, Name); @@ -4187,14 +4194,11 @@ if (llvm::sys::fs::exists(Twine(P))) return P.str(); - for (const std::string : TC.getFilePaths()) { -if (Dir.empty()) - continue; -SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); -llvm::sys::path::append(P, Name); -if (llvm::sys::fs::exists(Twine(P))) - return P.str(); - } + if (Optional D = FindPath(TC.getLibraryPaths())) +return *D; + + if (Optional D = FindPath(TC.getFilePaths())) +return *D; return Name; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits