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");
41*2b09dedaSRaphael Isemann   // If the path is in the libc++ include directory use it as the found libc++
42*2b09dedaSRaphael Isemann   // path. Ignore subdirectories such as /c++/v1/experimental as those don't
43*2b09dedaSRaphael Isemann   // need to be specified in the header search.
44*2b09dedaSRaphael Isemann   if (libcpp_regex.match(f.GetPath()) &&
45*2b09dedaSRaphael 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 
609379d19fSRaphael Isemann bool CppModuleConfiguration::hasValidConfig() {
619379d19fSRaphael Isemann   // We all these include directories to have a valid usable configuration.
629379d19fSRaphael Isemann   return m_c_inc.Valid() && m_std_inc.Valid();
639379d19fSRaphael Isemann }
649379d19fSRaphael Isemann 
659379d19fSRaphael Isemann CppModuleConfiguration::CppModuleConfiguration(
669379d19fSRaphael Isemann     const FileSpecList &support_files) {
679379d19fSRaphael Isemann   // Analyze all files we were given to build the configuration.
68186f1c58SRaphael Isemann   bool error = !llvm::all_of(support_files,
699379d19fSRaphael Isemann                              std::bind(&CppModuleConfiguration::analyzeFile,
709379d19fSRaphael Isemann                                        this, std::placeholders::_1));
719379d19fSRaphael Isemann   // If we have a valid configuration at this point, set the
729379d19fSRaphael Isemann   // include directories and module list that should be used.
739379d19fSRaphael Isemann   if (!error && hasValidConfig()) {
749379d19fSRaphael Isemann     // Calculate the resource directory for LLDB.
759379d19fSRaphael Isemann     llvm::SmallString<256> resource_dir;
769379d19fSRaphael Isemann     llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
779379d19fSRaphael Isemann                             "include");
78adcd0268SBenjamin Kramer     m_resource_inc = std::string(resource_dir.str());
799379d19fSRaphael Isemann 
809379d19fSRaphael Isemann     // This order matches the way Clang orders these directories.
819379d19fSRaphael Isemann     m_include_dirs = {m_std_inc.Get(), m_resource_inc, m_c_inc.Get()};
829379d19fSRaphael Isemann     m_imported_modules = {"std"};
839379d19fSRaphael Isemann   }
849379d19fSRaphael Isemann }
85