xref: /llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.cpp (revision afee09c50c11da1ea6ba0b341a1c10dedeaf5be6)
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"
1362c7f035SArchibald Elliott #include "llvm/TargetParser/Triple.h"
14f190ce62SKazu Hirata #include <optional>
159379d19fSRaphael Isemann 
169379d19fSRaphael Isemann using namespace lldb_private;
179379d19fSRaphael Isemann 
189379d19fSRaphael Isemann bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
199379d19fSRaphael Isemann   // Setting for the first time always works.
209379d19fSRaphael Isemann   if (m_first) {
219379d19fSRaphael Isemann     m_path = path.str();
229379d19fSRaphael Isemann     m_valid = true;
239379d19fSRaphael Isemann     m_first = false;
249379d19fSRaphael Isemann     return true;
259379d19fSRaphael Isemann   }
269379d19fSRaphael Isemann   // Changing the path to the same value is fine.
279379d19fSRaphael Isemann   if (m_path == path)
289379d19fSRaphael Isemann     return true;
299379d19fSRaphael Isemann 
309379d19fSRaphael Isemann   // Changing the path after it was already set is not allowed.
319379d19fSRaphael Isemann   m_valid = false;
329379d19fSRaphael Isemann   return false;
339379d19fSRaphael Isemann }
349379d19fSRaphael Isemann 
351aab5e65SPavel Kosov static llvm::SmallVector<std::string, 2>
361aab5e65SPavel Kosov getTargetIncludePaths(const llvm::Triple &triple) {
371aab5e65SPavel Kosov   llvm::SmallVector<std::string, 2> paths;
381aab5e65SPavel Kosov   if (!triple.str().empty()) {
391aab5e65SPavel Kosov     paths.push_back("/usr/include/" + triple.str());
401aab5e65SPavel Kosov     if (!triple.getArchName().empty() ||
411aab5e65SPavel Kosov         triple.getOSAndEnvironmentName().empty())
421aab5e65SPavel Kosov       paths.push_back(("/usr/include/" + triple.getArchName() + "-" +
431aab5e65SPavel Kosov                        triple.getOSAndEnvironmentName())
441aab5e65SPavel Kosov                           .str());
451aab5e65SPavel Kosov   }
461aab5e65SPavel Kosov   return paths;
471aab5e65SPavel Kosov }
481aab5e65SPavel Kosov 
491aab5e65SPavel Kosov /// Returns the include path matching the given pattern for the given file
508b5c302eSKazu Hirata /// path (or std::nullopt if the path doesn't match the pattern).
512fe83274SKazu Hirata static std::optional<llvm::StringRef>
521aab5e65SPavel Kosov guessIncludePath(llvm::StringRef path_to_file, llvm::StringRef pattern) {
531aab5e65SPavel Kosov   if (pattern.empty())
54343523d0SKazu Hirata     return std::nullopt;
551aab5e65SPavel Kosov   size_t pos = path_to_file.find(pattern);
561aab5e65SPavel Kosov   if (pos == llvm::StringRef::npos)
57343523d0SKazu Hirata     return std::nullopt;
581aab5e65SPavel Kosov 
591aab5e65SPavel Kosov   return path_to_file.substr(0, pos + pattern.size());
601aab5e65SPavel Kosov }
611aab5e65SPavel Kosov 
621aab5e65SPavel Kosov bool CppModuleConfiguration::analyzeFile(const FileSpec &f,
631aab5e65SPavel Kosov                                          const llvm::Triple &triple) {
6408f90e3dSRaphael Isemann   using namespace llvm::sys::path;
6508f90e3dSRaphael Isemann   // Convert to slashes to make following operations simpler.
6608f90e3dSRaphael Isemann   std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
679379d19fSRaphael Isemann   llvm::StringRef posix_dir(dir_buffer);
689379d19fSRaphael Isemann 
699379d19fSRaphael Isemann   // Check for /c++/vX/ that is used by libc++.
709379d19fSRaphael Isemann   static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");
712b09dedaSRaphael Isemann   // If the path is in the libc++ include directory use it as the found libc++
722b09dedaSRaphael Isemann   // path. Ignore subdirectories such as /c++/v1/experimental as those don't
732b09dedaSRaphael Isemann   // need to be specified in the header search.
74*afee09c5SVladislav Dzhidzhoev   if (libcpp_regex.match(convert_to_slash(f.GetPath())) &&
75744f3891SKazu Hirata       parent_path(posix_dir, Style::posix).ends_with("c++")) {
761aab5e65SPavel Kosov     if (!m_std_inc.TrySet(posix_dir))
771aab5e65SPavel Kosov       return false;
781aab5e65SPavel Kosov     if (triple.str().empty())
791aab5e65SPavel Kosov       return true;
801aab5e65SPavel Kosov 
811aab5e65SPavel Kosov     posix_dir.consume_back("c++/v1");
821aab5e65SPavel Kosov     // Check if this is a target-specific libc++ include directory.
831aab5e65SPavel Kosov     return m_std_target_inc.TrySet(
841aab5e65SPavel Kosov         (posix_dir + triple.str() + "/c++/v1").str());
859379d19fSRaphael Isemann   }
869379d19fSRaphael Isemann 
872fe83274SKazu Hirata   std::optional<llvm::StringRef> inc_path;
881aab5e65SPavel Kosov   // Target specific paths contains /usr/include, so we check them first
891aab5e65SPavel Kosov   for (auto &path : getTargetIncludePaths(triple)) {
901aab5e65SPavel Kosov     if ((inc_path = guessIncludePath(posix_dir, path)))
911aab5e65SPavel Kosov       return m_c_target_inc.TrySet(*inc_path);
921aab5e65SPavel Kosov   }
931aab5e65SPavel Kosov   if ((inc_path = guessIncludePath(posix_dir, "/usr/include")))
941aab5e65SPavel Kosov     return m_c_inc.TrySet(*inc_path);
959379d19fSRaphael Isemann 
969379d19fSRaphael Isemann   // File wasn't interesting, continue analyzing.
979379d19fSRaphael Isemann   return true;
989379d19fSRaphael Isemann }
999379d19fSRaphael Isemann 
10099b7b41eSRaphael Isemann /// Utility function for just appending two paths.
10199b7b41eSRaphael Isemann static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
10299b7b41eSRaphael Isemann   llvm::SmallString<256> result(lhs);
10399b7b41eSRaphael Isemann   llvm::sys::path::append(result, rhs);
10499b7b41eSRaphael Isemann   return std::string(result);
10599b7b41eSRaphael Isemann }
10699b7b41eSRaphael Isemann 
1079379d19fSRaphael Isemann bool CppModuleConfiguration::hasValidConfig() {
10899b7b41eSRaphael Isemann   // We need to have a C and C++ include dir for a valid configuration.
10999b7b41eSRaphael Isemann   if (!m_c_inc.Valid() || !m_std_inc.Valid())
11099b7b41eSRaphael Isemann     return false;
11199b7b41eSRaphael Isemann 
11299b7b41eSRaphael Isemann   // Do some basic sanity checks on the directories that we don't activate
11399b7b41eSRaphael Isemann   // the module when it's clear that it's not usable.
11499b7b41eSRaphael Isemann   const std::vector<std::string> files_to_check = {
11599b7b41eSRaphael Isemann       // * Check that the C library contains at least one random C standard
11699b7b41eSRaphael Isemann       //   library header.
11799b7b41eSRaphael Isemann       MakePath(m_c_inc.Get(), "stdio.h"),
11899b7b41eSRaphael Isemann       // * Without a libc++ modulemap file we can't have a 'std' module that
11999b7b41eSRaphael Isemann       //   could be imported.
12099b7b41eSRaphael Isemann       MakePath(m_std_inc.Get(), "module.modulemap"),
12199b7b41eSRaphael Isemann       // * Check for a random libc++ header (vector in this case) that has to
12299b7b41eSRaphael Isemann       //   exist in a working libc++ setup.
12399b7b41eSRaphael Isemann       MakePath(m_std_inc.Get(), "vector"),
12499b7b41eSRaphael Isemann   };
12599b7b41eSRaphael Isemann 
12699b7b41eSRaphael Isemann   for (llvm::StringRef file_to_check : files_to_check) {
12799b7b41eSRaphael Isemann     if (!FileSystem::Instance().Exists(file_to_check))
12899b7b41eSRaphael Isemann       return false;
12999b7b41eSRaphael Isemann   }
13099b7b41eSRaphael Isemann 
13199b7b41eSRaphael Isemann   return true;
1329379d19fSRaphael Isemann }
1339379d19fSRaphael Isemann 
1349379d19fSRaphael Isemann CppModuleConfiguration::CppModuleConfiguration(
1351aab5e65SPavel Kosov     const FileSpecList &support_files, const llvm::Triple &triple) {
1369379d19fSRaphael Isemann   // Analyze all files we were given to build the configuration.
137917b404eSAdrian Prantl   bool error = !llvm::all_of(support_files, [&](auto &file) {
138917b404eSAdrian Prantl     return CppModuleConfiguration::analyzeFile(file, triple);
139917b404eSAdrian Prantl   });
1409379d19fSRaphael Isemann   // If we have a valid configuration at this point, set the
1419379d19fSRaphael Isemann   // include directories and module list that should be used.
1429379d19fSRaphael Isemann   if (!error && hasValidConfig()) {
1439379d19fSRaphael Isemann     // Calculate the resource directory for LLDB.
1449379d19fSRaphael Isemann     llvm::SmallString<256> resource_dir;
1459379d19fSRaphael Isemann     llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
1469379d19fSRaphael Isemann                             "include");
147adcd0268SBenjamin Kramer     m_resource_inc = std::string(resource_dir.str());
1489379d19fSRaphael Isemann 
1499379d19fSRaphael Isemann     // This order matches the way Clang orders these directories.
15099b7b41eSRaphael Isemann     m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
15199b7b41eSRaphael Isemann                       m_c_inc.Get().str()};
1521aab5e65SPavel Kosov     if (m_c_target_inc.Valid())
1531aab5e65SPavel Kosov       m_include_dirs.push_back(m_c_target_inc.Get().str());
1541aab5e65SPavel Kosov     if (m_std_target_inc.Valid())
1551aab5e65SPavel Kosov       m_include_dirs.push_back(m_std_target_inc.Get().str());
1569379d19fSRaphael Isemann     m_imported_modules = {"std"};
1579379d19fSRaphael Isemann   }
1589379d19fSRaphael Isemann }
159