xref: /src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1ead24645SDimitry Andric //===-- CppModuleConfiguration.cpp ----------------------------------------===//
2ead24645SDimitry Andric //
3ead24645SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ead24645SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5ead24645SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ead24645SDimitry Andric //
7ead24645SDimitry Andric //===----------------------------------------------------------------------===//
8ead24645SDimitry Andric 
9ead24645SDimitry Andric #include "CppModuleConfiguration.h"
10ead24645SDimitry Andric 
11ead24645SDimitry Andric #include "ClangHost.h"
12ead24645SDimitry Andric #include "lldb/Host/FileSystem.h"
137fa27ce4SDimitry Andric #include "llvm/TargetParser/Triple.h"
14e3b55780SDimitry Andric #include <optional>
15ead24645SDimitry Andric 
16ead24645SDimitry Andric using namespace lldb_private;
17ead24645SDimitry Andric 
TrySet(llvm::StringRef path)18ead24645SDimitry Andric bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
19ead24645SDimitry Andric   // Setting for the first time always works.
20ead24645SDimitry Andric   if (m_first) {
21ead24645SDimitry Andric     m_path = path.str();
22ead24645SDimitry Andric     m_valid = true;
23ead24645SDimitry Andric     m_first = false;
24ead24645SDimitry Andric     return true;
25ead24645SDimitry Andric   }
26ead24645SDimitry Andric   // Changing the path to the same value is fine.
27ead24645SDimitry Andric   if (m_path == path)
28ead24645SDimitry Andric     return true;
29ead24645SDimitry Andric 
30ead24645SDimitry Andric   // Changing the path after it was already set is not allowed.
31ead24645SDimitry Andric   m_valid = false;
32ead24645SDimitry Andric   return false;
33ead24645SDimitry Andric }
34ead24645SDimitry Andric 
35f65dcba8SDimitry Andric static llvm::SmallVector<std::string, 2>
getTargetIncludePaths(const llvm::Triple & triple)36f65dcba8SDimitry Andric getTargetIncludePaths(const llvm::Triple &triple) {
37f65dcba8SDimitry Andric   llvm::SmallVector<std::string, 2> paths;
38f65dcba8SDimitry Andric   if (!triple.str().empty()) {
39f65dcba8SDimitry Andric     paths.push_back("/usr/include/" + triple.str());
40f65dcba8SDimitry Andric     if (!triple.getArchName().empty() ||
41f65dcba8SDimitry Andric         triple.getOSAndEnvironmentName().empty())
42f65dcba8SDimitry Andric       paths.push_back(("/usr/include/" + triple.getArchName() + "-" +
43f65dcba8SDimitry Andric                        triple.getOSAndEnvironmentName())
44f65dcba8SDimitry Andric                           .str());
45f65dcba8SDimitry Andric   }
46f65dcba8SDimitry Andric   return paths;
47f65dcba8SDimitry Andric }
48f65dcba8SDimitry Andric 
49f65dcba8SDimitry Andric /// Returns the include path matching the given pattern for the given file
50e3b55780SDimitry Andric /// path (or std::nullopt if the path doesn't match the pattern).
51e3b55780SDimitry Andric static std::optional<llvm::StringRef>
guessIncludePath(llvm::StringRef path_to_file,llvm::StringRef pattern)52f65dcba8SDimitry Andric guessIncludePath(llvm::StringRef path_to_file, llvm::StringRef pattern) {
53f65dcba8SDimitry Andric   if (pattern.empty())
54e3b55780SDimitry Andric     return std::nullopt;
55f65dcba8SDimitry Andric   size_t pos = path_to_file.find(pattern);
56f65dcba8SDimitry Andric   if (pos == llvm::StringRef::npos)
57e3b55780SDimitry Andric     return std::nullopt;
58f65dcba8SDimitry Andric 
59f65dcba8SDimitry Andric   return path_to_file.substr(0, pos + pattern.size());
60f65dcba8SDimitry Andric }
61f65dcba8SDimitry Andric 
analyzeFile(const FileSpec & f,const llvm::Triple & triple)62f65dcba8SDimitry Andric bool CppModuleConfiguration::analyzeFile(const FileSpec &f,
63f65dcba8SDimitry Andric                                          const llvm::Triple &triple) {
64ead24645SDimitry Andric   using namespace llvm::sys::path;
65ead24645SDimitry Andric   // Convert to slashes to make following operations simpler.
66ead24645SDimitry Andric   std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
67ead24645SDimitry Andric   llvm::StringRef posix_dir(dir_buffer);
68ead24645SDimitry Andric 
69ead24645SDimitry Andric   // Check for /c++/vX/ that is used by libc++.
70ead24645SDimitry Andric   static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");
71b60736ecSDimitry Andric   // If the path is in the libc++ include directory use it as the found libc++
72b60736ecSDimitry Andric   // path. Ignore subdirectories such as /c++/v1/experimental as those don't
73b60736ecSDimitry Andric   // need to be specified in the header search.
74ac9a064cSDimitry Andric   if (libcpp_regex.match(convert_to_slash(f.GetPath())) &&
75312c0ed1SDimitry Andric       parent_path(posix_dir, Style::posix).ends_with("c++")) {
76f65dcba8SDimitry Andric     if (!m_std_inc.TrySet(posix_dir))
77f65dcba8SDimitry Andric       return false;
78f65dcba8SDimitry Andric     if (triple.str().empty())
79f65dcba8SDimitry Andric       return true;
80f65dcba8SDimitry Andric 
81f65dcba8SDimitry Andric     posix_dir.consume_back("c++/v1");
82f65dcba8SDimitry Andric     // Check if this is a target-specific libc++ include directory.
83f65dcba8SDimitry Andric     return m_std_target_inc.TrySet(
84f65dcba8SDimitry Andric         (posix_dir + triple.str() + "/c++/v1").str());
85ead24645SDimitry Andric   }
86ead24645SDimitry Andric 
87e3b55780SDimitry Andric   std::optional<llvm::StringRef> inc_path;
88f65dcba8SDimitry Andric   // Target specific paths contains /usr/include, so we check them first
89f65dcba8SDimitry Andric   for (auto &path : getTargetIncludePaths(triple)) {
90f65dcba8SDimitry Andric     if ((inc_path = guessIncludePath(posix_dir, path)))
91f65dcba8SDimitry Andric       return m_c_target_inc.TrySet(*inc_path);
92f65dcba8SDimitry Andric   }
93f65dcba8SDimitry Andric   if ((inc_path = guessIncludePath(posix_dir, "/usr/include")))
94f65dcba8SDimitry Andric     return m_c_inc.TrySet(*inc_path);
95ead24645SDimitry Andric 
96ead24645SDimitry Andric   // File wasn't interesting, continue analyzing.
97ead24645SDimitry Andric   return true;
98ead24645SDimitry Andric }
99ead24645SDimitry Andric 
100b60736ecSDimitry Andric /// Utility function for just appending two paths.
MakePath(llvm::StringRef lhs,llvm::StringRef rhs)101b60736ecSDimitry Andric static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
102b60736ecSDimitry Andric   llvm::SmallString<256> result(lhs);
103b60736ecSDimitry Andric   llvm::sys::path::append(result, rhs);
104b60736ecSDimitry Andric   return std::string(result);
105b60736ecSDimitry Andric }
106b60736ecSDimitry Andric 
hasValidConfig()107ead24645SDimitry Andric bool CppModuleConfiguration::hasValidConfig() {
108b60736ecSDimitry Andric   // We need to have a C and C++ include dir for a valid configuration.
109b60736ecSDimitry Andric   if (!m_c_inc.Valid() || !m_std_inc.Valid())
110b60736ecSDimitry Andric     return false;
111b60736ecSDimitry Andric 
112b60736ecSDimitry Andric   // Do some basic sanity checks on the directories that we don't activate
113b60736ecSDimitry Andric   // the module when it's clear that it's not usable.
114b60736ecSDimitry Andric   const std::vector<std::string> files_to_check = {
115b60736ecSDimitry Andric       // * Check that the C library contains at least one random C standard
116b60736ecSDimitry Andric       //   library header.
117b60736ecSDimitry Andric       MakePath(m_c_inc.Get(), "stdio.h"),
118b60736ecSDimitry Andric       // * Without a libc++ modulemap file we can't have a 'std' module that
119b60736ecSDimitry Andric       //   could be imported.
120b60736ecSDimitry Andric       MakePath(m_std_inc.Get(), "module.modulemap"),
121b60736ecSDimitry Andric       // * Check for a random libc++ header (vector in this case) that has to
122b60736ecSDimitry Andric       //   exist in a working libc++ setup.
123b60736ecSDimitry Andric       MakePath(m_std_inc.Get(), "vector"),
124b60736ecSDimitry Andric   };
125b60736ecSDimitry Andric 
126b60736ecSDimitry Andric   for (llvm::StringRef file_to_check : files_to_check) {
127b60736ecSDimitry Andric     if (!FileSystem::Instance().Exists(file_to_check))
128b60736ecSDimitry Andric       return false;
129b60736ecSDimitry Andric   }
130b60736ecSDimitry Andric 
131b60736ecSDimitry Andric   return true;
132ead24645SDimitry Andric }
133ead24645SDimitry Andric 
CppModuleConfiguration(const FileSpecList & support_files,const llvm::Triple & triple)134ead24645SDimitry Andric CppModuleConfiguration::CppModuleConfiguration(
135f65dcba8SDimitry Andric     const FileSpecList &support_files, const llvm::Triple &triple) {
136ead24645SDimitry Andric   // Analyze all files we were given to build the configuration.
137aca2e42cSDimitry Andric   bool error = !llvm::all_of(support_files, [&](auto &file) {
138aca2e42cSDimitry Andric     return CppModuleConfiguration::analyzeFile(file, triple);
139aca2e42cSDimitry Andric   });
140ead24645SDimitry Andric   // If we have a valid configuration at this point, set the
141ead24645SDimitry Andric   // include directories and module list that should be used.
142ead24645SDimitry Andric   if (!error && hasValidConfig()) {
143ead24645SDimitry Andric     // Calculate the resource directory for LLDB.
144ead24645SDimitry Andric     llvm::SmallString<256> resource_dir;
145ead24645SDimitry Andric     llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
146ead24645SDimitry Andric                             "include");
147cfca06d7SDimitry Andric     m_resource_inc = std::string(resource_dir.str());
148ead24645SDimitry Andric 
149ead24645SDimitry Andric     // This order matches the way Clang orders these directories.
150b60736ecSDimitry Andric     m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
151b60736ecSDimitry Andric                       m_c_inc.Get().str()};
152f65dcba8SDimitry Andric     if (m_c_target_inc.Valid())
153f65dcba8SDimitry Andric       m_include_dirs.push_back(m_c_target_inc.Get().str());
154f65dcba8SDimitry Andric     if (m_std_target_inc.Valid())
155f65dcba8SDimitry Andric       m_include_dirs.push_back(m_std_target_inc.Get().str());
156ead24645SDimitry Andric     m_imported_modules = {"std"};
157ead24645SDimitry Andric   }
158ead24645SDimitry Andric }
159