1//===-- CppModuleConfiguration.cpp ----------------------------------------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#include "CppModuleConfiguration.h" 10 11#include "ClangHost.h" 12#include "lldb/Host/FileSystem.h" 13 14using namespace lldb_private; 15 16bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) { 17 // Setting for the first time always works. 18 if (m_first) { 19 m_path = path.str(); 20 m_valid = true; 21 m_first = false; 22 return true; 23 } 24 // Changing the path to the same value is fine. 25 if (m_path == path) 26 return true; 27 28 // Changing the path after it was already set is not allowed. 29 m_valid = false; 30 return false; 31} 32 33bool CppModuleConfiguration::analyzeFile(const FileSpec &f) { 34 using namespace llvm::sys::path; 35 // Convert to slashes to make following operations simpler. 36 std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef()); 37 llvm::StringRef posix_dir(dir_buffer); 38 39 // Check for /c++/vX/ that is used by libc++. 40 static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex"); 41 if (libcpp_regex.match(f.GetPath())) { 42 // Strip away libc++'s /experimental directory if there is one. 43 posix_dir.consume_back("/experimental"); 44 return m_std_inc.TrySet(posix_dir); 45 } 46 47 // Check for /usr/include. On Linux this might be /usr/include/bits, so 48 // we should remove that '/bits' suffix to get the actual include directory. 49 if (posix_dir.endswith("/usr/include/bits")) 50 posix_dir.consume_back("/bits"); 51 if (posix_dir.endswith("/usr/include")) 52 return m_c_inc.TrySet(posix_dir); 53 54 // File wasn't interesting, continue analyzing. 55 return true; 56} 57 58bool CppModuleConfiguration::hasValidConfig() { 59 // We all these include directories to have a valid usable configuration. 60 return m_c_inc.Valid() && m_std_inc.Valid(); 61} 62 63CppModuleConfiguration::CppModuleConfiguration( 64 const FileSpecList &support_files) { 65 // Analyze all files we were given to build the configuration. 66 bool error = !llvm::all_of(support_files, 67 std::bind(&CppModuleConfiguration::analyzeFile, 68 this, std::placeholders::_1)); 69 // If we have a valid configuration at this point, set the 70 // include directories and module list that should be used. 71 if (!error && hasValidConfig()) { 72 // Calculate the resource directory for LLDB. 73 llvm::SmallString<256> resource_dir; 74 llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(), 75 "include"); 76 m_resource_inc = resource_dir.str(); 77 78 // This order matches the way Clang orders these directories. 79 m_include_dirs = {m_std_inc.Get(), m_resource_inc, m_c_inc.Get()}; 80 m_imported_modules = {"std"}; 81 } 82} 83