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"
139379d19fSRaphael Isemann 
149379d19fSRaphael Isemann using namespace lldb_private;
159379d19fSRaphael Isemann 
169379d19fSRaphael Isemann bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
179379d19fSRaphael Isemann   // Setting for the first time always works.
189379d19fSRaphael Isemann   if (m_first) {
199379d19fSRaphael Isemann     m_path = path.str();
209379d19fSRaphael Isemann     m_valid = true;
219379d19fSRaphael Isemann     m_first = false;
229379d19fSRaphael Isemann     return true;
239379d19fSRaphael Isemann   }
249379d19fSRaphael Isemann   // Changing the path to the same value is fine.
259379d19fSRaphael Isemann   if (m_path == path)
269379d19fSRaphael Isemann     return true;
279379d19fSRaphael Isemann 
289379d19fSRaphael Isemann   // Changing the path after it was already set is not allowed.
299379d19fSRaphael Isemann   m_valid = false;
309379d19fSRaphael Isemann   return false;
319379d19fSRaphael Isemann }
329379d19fSRaphael Isemann 
339379d19fSRaphael Isemann bool CppModuleConfiguration::analyzeFile(const FileSpec &f) {
3408f90e3dSRaphael Isemann   using namespace llvm::sys::path;
3508f90e3dSRaphael Isemann   // Convert to slashes to make following operations simpler.
3608f90e3dSRaphael Isemann   std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
379379d19fSRaphael Isemann   llvm::StringRef posix_dir(dir_buffer);
389379d19fSRaphael Isemann 
399379d19fSRaphael Isemann   // Check for /c++/vX/ that is used by libc++.
409379d19fSRaphael Isemann   static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");
412b09dedaSRaphael Isemann   // If the path is in the libc++ include directory use it as the found libc++
422b09dedaSRaphael Isemann   // path. Ignore subdirectories such as /c++/v1/experimental as those don't
432b09dedaSRaphael Isemann   // need to be specified in the header search.
442b09dedaSRaphael Isemann   if (libcpp_regex.match(f.GetPath()) &&
452b09dedaSRaphael Isemann       parent_path(posix_dir, Style::posix).endswith("c++")) {
469379d19fSRaphael Isemann     return m_std_inc.TrySet(posix_dir);
479379d19fSRaphael Isemann   }
489379d19fSRaphael Isemann 
499379d19fSRaphael Isemann   // Check for /usr/include. On Linux this might be /usr/include/bits, so
509379d19fSRaphael Isemann   // we should remove that '/bits' suffix to get the actual include directory.
519379d19fSRaphael Isemann   if (posix_dir.endswith("/usr/include/bits"))
529379d19fSRaphael Isemann     posix_dir.consume_back("/bits");
539379d19fSRaphael Isemann   if (posix_dir.endswith("/usr/include"))
549379d19fSRaphael Isemann     return m_c_inc.TrySet(posix_dir);
559379d19fSRaphael Isemann 
569379d19fSRaphael Isemann   // File wasn't interesting, continue analyzing.
579379d19fSRaphael Isemann   return true;
589379d19fSRaphael Isemann }
599379d19fSRaphael Isemann 
60*99b7b41eSRaphael Isemann /// Utility function for just appending two paths.
61*99b7b41eSRaphael Isemann static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
62*99b7b41eSRaphael Isemann   llvm::SmallString<256> result(lhs);
63*99b7b41eSRaphael Isemann   llvm::sys::path::append(result, rhs);
64*99b7b41eSRaphael Isemann   return std::string(result);
65*99b7b41eSRaphael Isemann }
66*99b7b41eSRaphael Isemann 
679379d19fSRaphael Isemann bool CppModuleConfiguration::hasValidConfig() {
68*99b7b41eSRaphael Isemann   // We need to have a C and C++ include dir for a valid configuration.
69*99b7b41eSRaphael Isemann   if (!m_c_inc.Valid() || !m_std_inc.Valid())
70*99b7b41eSRaphael Isemann     return false;
71*99b7b41eSRaphael Isemann 
72*99b7b41eSRaphael Isemann   // Do some basic sanity checks on the directories that we don't activate
73*99b7b41eSRaphael Isemann   // the module when it's clear that it's not usable.
74*99b7b41eSRaphael Isemann   const std::vector<std::string> files_to_check = {
75*99b7b41eSRaphael Isemann       // * Check that the C library contains at least one random C standard
76*99b7b41eSRaphael Isemann       //   library header.
77*99b7b41eSRaphael Isemann       MakePath(m_c_inc.Get(), "stdio.h"),
78*99b7b41eSRaphael Isemann       // * Without a libc++ modulemap file we can't have a 'std' module that
79*99b7b41eSRaphael Isemann       //   could be imported.
80*99b7b41eSRaphael Isemann       MakePath(m_std_inc.Get(), "module.modulemap"),
81*99b7b41eSRaphael Isemann       // * Check for a random libc++ header (vector in this case) that has to
82*99b7b41eSRaphael Isemann       //   exist in a working libc++ setup.
83*99b7b41eSRaphael Isemann       MakePath(m_std_inc.Get(), "vector"),
84*99b7b41eSRaphael Isemann   };
85*99b7b41eSRaphael Isemann 
86*99b7b41eSRaphael Isemann   for (llvm::StringRef file_to_check : files_to_check) {
87*99b7b41eSRaphael Isemann     if (!FileSystem::Instance().Exists(file_to_check))
88*99b7b41eSRaphael Isemann       return false;
89*99b7b41eSRaphael Isemann   }
90*99b7b41eSRaphael Isemann 
91*99b7b41eSRaphael Isemann   return true;
929379d19fSRaphael Isemann }
939379d19fSRaphael Isemann 
949379d19fSRaphael Isemann CppModuleConfiguration::CppModuleConfiguration(
959379d19fSRaphael Isemann     const FileSpecList &support_files) {
969379d19fSRaphael Isemann   // Analyze all files we were given to build the configuration.
97186f1c58SRaphael Isemann   bool error = !llvm::all_of(support_files,
989379d19fSRaphael Isemann                              std::bind(&CppModuleConfiguration::analyzeFile,
999379d19fSRaphael Isemann                                        this, std::placeholders::_1));
1009379d19fSRaphael Isemann   // If we have a valid configuration at this point, set the
1019379d19fSRaphael Isemann   // include directories and module list that should be used.
1029379d19fSRaphael Isemann   if (!error && hasValidConfig()) {
1039379d19fSRaphael Isemann     // Calculate the resource directory for LLDB.
1049379d19fSRaphael Isemann     llvm::SmallString<256> resource_dir;
1059379d19fSRaphael Isemann     llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
1069379d19fSRaphael Isemann                             "include");
107adcd0268SBenjamin Kramer     m_resource_inc = std::string(resource_dir.str());
1089379d19fSRaphael Isemann 
1099379d19fSRaphael Isemann     // This order matches the way Clang orders these directories.
110*99b7b41eSRaphael Isemann     m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
111*99b7b41eSRaphael Isemann                       m_c_inc.Get().str()};
1129379d19fSRaphael Isemann     m_imported_modules = {"std"};
1139379d19fSRaphael Isemann   }
1149379d19fSRaphael Isemann }
115