19dba64beSDimitry Andric //===-- CppModuleConfiguration.cpp ----------------------------------------===//
29dba64beSDimitry Andric //
39dba64beSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49dba64beSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
59dba64beSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69dba64beSDimitry Andric //
79dba64beSDimitry Andric //===----------------------------------------------------------------------===//
89dba64beSDimitry Andric
99dba64beSDimitry Andric #include "CppModuleConfiguration.h"
109dba64beSDimitry Andric
119dba64beSDimitry Andric #include "ClangHost.h"
129dba64beSDimitry Andric #include "lldb/Host/FileSystem.h"
139dba64beSDimitry Andric
149dba64beSDimitry Andric using namespace lldb_private;
159dba64beSDimitry Andric
TrySet(llvm::StringRef path)169dba64beSDimitry Andric bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
179dba64beSDimitry Andric // Setting for the first time always works.
189dba64beSDimitry Andric if (m_first) {
199dba64beSDimitry Andric m_path = path.str();
209dba64beSDimitry Andric m_valid = true;
219dba64beSDimitry Andric m_first = false;
229dba64beSDimitry Andric return true;
239dba64beSDimitry Andric }
249dba64beSDimitry Andric // Changing the path to the same value is fine.
259dba64beSDimitry Andric if (m_path == path)
269dba64beSDimitry Andric return true;
279dba64beSDimitry Andric
289dba64beSDimitry Andric // Changing the path after it was already set is not allowed.
299dba64beSDimitry Andric m_valid = false;
309dba64beSDimitry Andric return false;
319dba64beSDimitry Andric }
329dba64beSDimitry Andric
analyzeFile(const FileSpec & f)339dba64beSDimitry Andric bool CppModuleConfiguration::analyzeFile(const FileSpec &f) {
349dba64beSDimitry Andric using namespace llvm::sys::path;
359dba64beSDimitry Andric // Convert to slashes to make following operations simpler.
369dba64beSDimitry Andric std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
379dba64beSDimitry Andric llvm::StringRef posix_dir(dir_buffer);
389dba64beSDimitry Andric
399dba64beSDimitry Andric // Check for /c++/vX/ that is used by libc++.
409dba64beSDimitry Andric static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");
41*af732203SDimitry Andric // If the path is in the libc++ include directory use it as the found libc++
42*af732203SDimitry Andric // path. Ignore subdirectories such as /c++/v1/experimental as those don't
43*af732203SDimitry Andric // need to be specified in the header search.
44*af732203SDimitry Andric if (libcpp_regex.match(f.GetPath()) &&
45*af732203SDimitry Andric parent_path(posix_dir, Style::posix).endswith("c++")) {
469dba64beSDimitry Andric return m_std_inc.TrySet(posix_dir);
479dba64beSDimitry Andric }
489dba64beSDimitry Andric
499dba64beSDimitry Andric // Check for /usr/include. On Linux this might be /usr/include/bits, so
509dba64beSDimitry Andric // we should remove that '/bits' suffix to get the actual include directory.
519dba64beSDimitry Andric if (posix_dir.endswith("/usr/include/bits"))
529dba64beSDimitry Andric posix_dir.consume_back("/bits");
539dba64beSDimitry Andric if (posix_dir.endswith("/usr/include"))
549dba64beSDimitry Andric return m_c_inc.TrySet(posix_dir);
559dba64beSDimitry Andric
569dba64beSDimitry Andric // File wasn't interesting, continue analyzing.
579dba64beSDimitry Andric return true;
589dba64beSDimitry Andric }
599dba64beSDimitry Andric
60*af732203SDimitry Andric /// Utility function for just appending two paths.
MakePath(llvm::StringRef lhs,llvm::StringRef rhs)61*af732203SDimitry Andric static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
62*af732203SDimitry Andric llvm::SmallString<256> result(lhs);
63*af732203SDimitry Andric llvm::sys::path::append(result, rhs);
64*af732203SDimitry Andric return std::string(result);
65*af732203SDimitry Andric }
66*af732203SDimitry Andric
hasValidConfig()679dba64beSDimitry Andric bool CppModuleConfiguration::hasValidConfig() {
68*af732203SDimitry Andric // We need to have a C and C++ include dir for a valid configuration.
69*af732203SDimitry Andric if (!m_c_inc.Valid() || !m_std_inc.Valid())
70*af732203SDimitry Andric return false;
71*af732203SDimitry Andric
72*af732203SDimitry Andric // Do some basic sanity checks on the directories that we don't activate
73*af732203SDimitry Andric // the module when it's clear that it's not usable.
74*af732203SDimitry Andric const std::vector<std::string> files_to_check = {
75*af732203SDimitry Andric // * Check that the C library contains at least one random C standard
76*af732203SDimitry Andric // library header.
77*af732203SDimitry Andric MakePath(m_c_inc.Get(), "stdio.h"),
78*af732203SDimitry Andric // * Without a libc++ modulemap file we can't have a 'std' module that
79*af732203SDimitry Andric // could be imported.
80*af732203SDimitry Andric MakePath(m_std_inc.Get(), "module.modulemap"),
81*af732203SDimitry Andric // * Check for a random libc++ header (vector in this case) that has to
82*af732203SDimitry Andric // exist in a working libc++ setup.
83*af732203SDimitry Andric MakePath(m_std_inc.Get(), "vector"),
84*af732203SDimitry Andric };
85*af732203SDimitry Andric
86*af732203SDimitry Andric for (llvm::StringRef file_to_check : files_to_check) {
87*af732203SDimitry Andric if (!FileSystem::Instance().Exists(file_to_check))
88*af732203SDimitry Andric return false;
89*af732203SDimitry Andric }
90*af732203SDimitry Andric
91*af732203SDimitry Andric return true;
929dba64beSDimitry Andric }
939dba64beSDimitry Andric
CppModuleConfiguration(const FileSpecList & support_files)949dba64beSDimitry Andric CppModuleConfiguration::CppModuleConfiguration(
959dba64beSDimitry Andric const FileSpecList &support_files) {
969dba64beSDimitry Andric // Analyze all files we were given to build the configuration.
979dba64beSDimitry Andric bool error = !llvm::all_of(support_files,
989dba64beSDimitry Andric std::bind(&CppModuleConfiguration::analyzeFile,
999dba64beSDimitry Andric this, std::placeholders::_1));
1009dba64beSDimitry Andric // If we have a valid configuration at this point, set the
1019dba64beSDimitry Andric // include directories and module list that should be used.
1029dba64beSDimitry Andric if (!error && hasValidConfig()) {
1039dba64beSDimitry Andric // Calculate the resource directory for LLDB.
1049dba64beSDimitry Andric llvm::SmallString<256> resource_dir;
1059dba64beSDimitry Andric llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
1069dba64beSDimitry Andric "include");
1075ffd83dbSDimitry Andric m_resource_inc = std::string(resource_dir.str());
1089dba64beSDimitry Andric
1099dba64beSDimitry Andric // This order matches the way Clang orders these directories.
110*af732203SDimitry Andric m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
111*af732203SDimitry Andric m_c_inc.Get().str()};
1129dba64beSDimitry Andric m_imported_modules = {"std"};
1139dba64beSDimitry Andric }
1149dba64beSDimitry Andric }
115