19379d19fSRaphael Isemann //===-- CppModuleConfiguration.cpp ----------------------------------------===//
29379d19fSRaphael Isemann //
39379d19fSRaphael Isemann // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49379d19fSRaphael Isemann // See https://llvm.org/LICENSE.txt for license information.
59379d19fSRaphael Isemann // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69379d19fSRaphael Isemann //
79379d19fSRaphael Isemann //===----------------------------------------------------------------------===//
89379d19fSRaphael Isemann
99379d19fSRaphael Isemann #include "CppModuleConfiguration.h"
109379d19fSRaphael Isemann
119379d19fSRaphael Isemann #include "ClangHost.h"
129379d19fSRaphael Isemann #include "lldb/Host/FileSystem.h"
13*1aab5e65SPavel Kosov #include "llvm/ADT/Triple.h"
149379d19fSRaphael Isemann
159379d19fSRaphael Isemann using namespace lldb_private;
169379d19fSRaphael Isemann
TrySet(llvm::StringRef path)179379d19fSRaphael Isemann bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
189379d19fSRaphael Isemann // Setting for the first time always works.
199379d19fSRaphael Isemann if (m_first) {
209379d19fSRaphael Isemann m_path = path.str();
219379d19fSRaphael Isemann m_valid = true;
229379d19fSRaphael Isemann m_first = false;
239379d19fSRaphael Isemann return true;
249379d19fSRaphael Isemann }
259379d19fSRaphael Isemann // Changing the path to the same value is fine.
269379d19fSRaphael Isemann if (m_path == path)
279379d19fSRaphael Isemann return true;
289379d19fSRaphael Isemann
299379d19fSRaphael Isemann // Changing the path after it was already set is not allowed.
309379d19fSRaphael Isemann m_valid = false;
319379d19fSRaphael Isemann return false;
329379d19fSRaphael Isemann }
339379d19fSRaphael Isemann
34*1aab5e65SPavel Kosov static llvm::SmallVector<std::string, 2>
getTargetIncludePaths(const llvm::Triple & triple)35*1aab5e65SPavel Kosov getTargetIncludePaths(const llvm::Triple &triple) {
36*1aab5e65SPavel Kosov llvm::SmallVector<std::string, 2> paths;
37*1aab5e65SPavel Kosov if (!triple.str().empty()) {
38*1aab5e65SPavel Kosov paths.push_back("/usr/include/" + triple.str());
39*1aab5e65SPavel Kosov if (!triple.getArchName().empty() ||
40*1aab5e65SPavel Kosov triple.getOSAndEnvironmentName().empty())
41*1aab5e65SPavel Kosov paths.push_back(("/usr/include/" + triple.getArchName() + "-" +
42*1aab5e65SPavel Kosov triple.getOSAndEnvironmentName())
43*1aab5e65SPavel Kosov .str());
44*1aab5e65SPavel Kosov }
45*1aab5e65SPavel Kosov return paths;
46*1aab5e65SPavel Kosov }
47*1aab5e65SPavel Kosov
48*1aab5e65SPavel Kosov /// Returns the include path matching the given pattern for the given file
49*1aab5e65SPavel Kosov /// path (or None if the path doesn't match the pattern).
50*1aab5e65SPavel Kosov static llvm::Optional<llvm::StringRef>
guessIncludePath(llvm::StringRef path_to_file,llvm::StringRef pattern)51*1aab5e65SPavel Kosov guessIncludePath(llvm::StringRef path_to_file, llvm::StringRef pattern) {
52*1aab5e65SPavel Kosov if (pattern.empty())
53*1aab5e65SPavel Kosov return llvm::NoneType();
54*1aab5e65SPavel Kosov size_t pos = path_to_file.find(pattern);
55*1aab5e65SPavel Kosov if (pos == llvm::StringRef::npos)
56*1aab5e65SPavel Kosov return llvm::NoneType();
57*1aab5e65SPavel Kosov
58*1aab5e65SPavel Kosov return path_to_file.substr(0, pos + pattern.size());
59*1aab5e65SPavel Kosov }
60*1aab5e65SPavel Kosov
analyzeFile(const FileSpec & f,const llvm::Triple & triple)61*1aab5e65SPavel Kosov bool CppModuleConfiguration::analyzeFile(const FileSpec &f,
62*1aab5e65SPavel Kosov const llvm::Triple &triple) {
6308f90e3dSRaphael Isemann using namespace llvm::sys::path;
6408f90e3dSRaphael Isemann // Convert to slashes to make following operations simpler.
6508f90e3dSRaphael Isemann std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
669379d19fSRaphael Isemann llvm::StringRef posix_dir(dir_buffer);
679379d19fSRaphael Isemann
689379d19fSRaphael Isemann // Check for /c++/vX/ that is used by libc++.
699379d19fSRaphael Isemann static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");
702b09dedaSRaphael Isemann // If the path is in the libc++ include directory use it as the found libc++
712b09dedaSRaphael Isemann // path. Ignore subdirectories such as /c++/v1/experimental as those don't
722b09dedaSRaphael Isemann // need to be specified in the header search.
732b09dedaSRaphael Isemann if (libcpp_regex.match(f.GetPath()) &&
742b09dedaSRaphael Isemann parent_path(posix_dir, Style::posix).endswith("c++")) {
75*1aab5e65SPavel Kosov if (!m_std_inc.TrySet(posix_dir))
76*1aab5e65SPavel Kosov return false;
77*1aab5e65SPavel Kosov if (triple.str().empty())
78*1aab5e65SPavel Kosov return true;
79*1aab5e65SPavel Kosov
80*1aab5e65SPavel Kosov posix_dir.consume_back("c++/v1");
81*1aab5e65SPavel Kosov // Check if this is a target-specific libc++ include directory.
82*1aab5e65SPavel Kosov return m_std_target_inc.TrySet(
83*1aab5e65SPavel Kosov (posix_dir + triple.str() + "/c++/v1").str());
849379d19fSRaphael Isemann }
859379d19fSRaphael Isemann
86*1aab5e65SPavel Kosov llvm::Optional<llvm::StringRef> inc_path;
87*1aab5e65SPavel Kosov // Target specific paths contains /usr/include, so we check them first
88*1aab5e65SPavel Kosov for (auto &path : getTargetIncludePaths(triple)) {
89*1aab5e65SPavel Kosov if ((inc_path = guessIncludePath(posix_dir, path)))
90*1aab5e65SPavel Kosov return m_c_target_inc.TrySet(*inc_path);
91*1aab5e65SPavel Kosov }
92*1aab5e65SPavel Kosov if ((inc_path = guessIncludePath(posix_dir, "/usr/include")))
93*1aab5e65SPavel Kosov return m_c_inc.TrySet(*inc_path);
949379d19fSRaphael Isemann
959379d19fSRaphael Isemann // File wasn't interesting, continue analyzing.
969379d19fSRaphael Isemann return true;
979379d19fSRaphael Isemann }
989379d19fSRaphael Isemann
9999b7b41eSRaphael Isemann /// Utility function for just appending two paths.
MakePath(llvm::StringRef lhs,llvm::StringRef rhs)10099b7b41eSRaphael Isemann static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
10199b7b41eSRaphael Isemann llvm::SmallString<256> result(lhs);
10299b7b41eSRaphael Isemann llvm::sys::path::append(result, rhs);
10399b7b41eSRaphael Isemann return std::string(result);
10499b7b41eSRaphael Isemann }
10599b7b41eSRaphael Isemann
hasValidConfig()1069379d19fSRaphael Isemann bool CppModuleConfiguration::hasValidConfig() {
10799b7b41eSRaphael Isemann // We need to have a C and C++ include dir for a valid configuration.
10899b7b41eSRaphael Isemann if (!m_c_inc.Valid() || !m_std_inc.Valid())
10999b7b41eSRaphael Isemann return false;
11099b7b41eSRaphael Isemann
11199b7b41eSRaphael Isemann // Do some basic sanity checks on the directories that we don't activate
11299b7b41eSRaphael Isemann // the module when it's clear that it's not usable.
11399b7b41eSRaphael Isemann const std::vector<std::string> files_to_check = {
11499b7b41eSRaphael Isemann // * Check that the C library contains at least one random C standard
11599b7b41eSRaphael Isemann // library header.
11699b7b41eSRaphael Isemann MakePath(m_c_inc.Get(), "stdio.h"),
11799b7b41eSRaphael Isemann // * Without a libc++ modulemap file we can't have a 'std' module that
11899b7b41eSRaphael Isemann // could be imported.
11999b7b41eSRaphael Isemann MakePath(m_std_inc.Get(), "module.modulemap"),
12099b7b41eSRaphael Isemann // * Check for a random libc++ header (vector in this case) that has to
12199b7b41eSRaphael Isemann // exist in a working libc++ setup.
12299b7b41eSRaphael Isemann MakePath(m_std_inc.Get(), "vector"),
12399b7b41eSRaphael Isemann };
12499b7b41eSRaphael Isemann
12599b7b41eSRaphael Isemann for (llvm::StringRef file_to_check : files_to_check) {
12699b7b41eSRaphael Isemann if (!FileSystem::Instance().Exists(file_to_check))
12799b7b41eSRaphael Isemann return false;
12899b7b41eSRaphael Isemann }
12999b7b41eSRaphael Isemann
13099b7b41eSRaphael Isemann return true;
1319379d19fSRaphael Isemann }
1329379d19fSRaphael Isemann
CppModuleConfiguration(const FileSpecList & support_files,const llvm::Triple & triple)1339379d19fSRaphael Isemann CppModuleConfiguration::CppModuleConfiguration(
134*1aab5e65SPavel Kosov const FileSpecList &support_files, const llvm::Triple &triple) {
1359379d19fSRaphael Isemann // Analyze all files we were given to build the configuration.
136186f1c58SRaphael Isemann bool error = !llvm::all_of(support_files,
1379379d19fSRaphael Isemann std::bind(&CppModuleConfiguration::analyzeFile,
138*1aab5e65SPavel Kosov this, std::placeholders::_1, triple));
1399379d19fSRaphael Isemann // If we have a valid configuration at this point, set the
1409379d19fSRaphael Isemann // include directories and module list that should be used.
1419379d19fSRaphael Isemann if (!error && hasValidConfig()) {
1429379d19fSRaphael Isemann // Calculate the resource directory for LLDB.
1439379d19fSRaphael Isemann llvm::SmallString<256> resource_dir;
1449379d19fSRaphael Isemann llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
1459379d19fSRaphael Isemann "include");
146adcd0268SBenjamin Kramer m_resource_inc = std::string(resource_dir.str());
1479379d19fSRaphael Isemann
1489379d19fSRaphael Isemann // This order matches the way Clang orders these directories.
14999b7b41eSRaphael Isemann m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
15099b7b41eSRaphael Isemann m_c_inc.Get().str()};
151*1aab5e65SPavel Kosov if (m_c_target_inc.Valid())
152*1aab5e65SPavel Kosov m_include_dirs.push_back(m_c_target_inc.Get().str());
153*1aab5e65SPavel Kosov if (m_std_target_inc.Valid())
154*1aab5e65SPavel Kosov m_include_dirs.push_back(m_std_target_inc.Get().str());
1559379d19fSRaphael Isemann m_imported_modules = {"std"};
1569379d19fSRaphael Isemann }
1579379d19fSRaphael Isemann }
158