Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.cpp
39648 views
//===-- CppModuleConfiguration.cpp ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "CppModuleConfiguration.h"910#include "ClangHost.h"11#include "lldb/Host/FileSystem.h"12#include "llvm/TargetParser/Triple.h"13#include <optional>1415using namespace lldb_private;1617bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {18// Setting for the first time always works.19if (m_first) {20m_path = path.str();21m_valid = true;22m_first = false;23return true;24}25// Changing the path to the same value is fine.26if (m_path == path)27return true;2829// Changing the path after it was already set is not allowed.30m_valid = false;31return false;32}3334static llvm::SmallVector<std::string, 2>35getTargetIncludePaths(const llvm::Triple &triple) {36llvm::SmallVector<std::string, 2> paths;37if (!triple.str().empty()) {38paths.push_back("/usr/include/" + triple.str());39if (!triple.getArchName().empty() ||40triple.getOSAndEnvironmentName().empty())41paths.push_back(("/usr/include/" + triple.getArchName() + "-" +42triple.getOSAndEnvironmentName())43.str());44}45return paths;46}4748/// Returns the include path matching the given pattern for the given file49/// path (or std::nullopt if the path doesn't match the pattern).50static std::optional<llvm::StringRef>51guessIncludePath(llvm::StringRef path_to_file, llvm::StringRef pattern) {52if (pattern.empty())53return std::nullopt;54size_t pos = path_to_file.find(pattern);55if (pos == llvm::StringRef::npos)56return std::nullopt;5758return path_to_file.substr(0, pos + pattern.size());59}6061bool CppModuleConfiguration::analyzeFile(const FileSpec &f,62const llvm::Triple &triple) {63using namespace llvm::sys::path;64// Convert to slashes to make following operations simpler.65std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());66llvm::StringRef posix_dir(dir_buffer);6768// Check for /c++/vX/ that is used by libc++.69static llvm::Regex libcpp_regex(R"regex(/c[+][+]/v[0-9]/)regex");70// If the path is in the libc++ include directory use it as the found libc++71// path. Ignore subdirectories such as /c++/v1/experimental as those don't72// need to be specified in the header search.73if (libcpp_regex.match(convert_to_slash(f.GetPath())) &&74parent_path(posix_dir, Style::posix).ends_with("c++")) {75if (!m_std_inc.TrySet(posix_dir))76return false;77if (triple.str().empty())78return true;7980posix_dir.consume_back("c++/v1");81// Check if this is a target-specific libc++ include directory.82return m_std_target_inc.TrySet(83(posix_dir + triple.str() + "/c++/v1").str());84}8586std::optional<llvm::StringRef> inc_path;87// Target specific paths contains /usr/include, so we check them first88for (auto &path : getTargetIncludePaths(triple)) {89if ((inc_path = guessIncludePath(posix_dir, path)))90return m_c_target_inc.TrySet(*inc_path);91}92if ((inc_path = guessIncludePath(posix_dir, "/usr/include")))93return m_c_inc.TrySet(*inc_path);9495// File wasn't interesting, continue analyzing.96return true;97}9899/// Utility function for just appending two paths.100static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {101llvm::SmallString<256> result(lhs);102llvm::sys::path::append(result, rhs);103return std::string(result);104}105106bool CppModuleConfiguration::hasValidConfig() {107// We need to have a C and C++ include dir for a valid configuration.108if (!m_c_inc.Valid() || !m_std_inc.Valid())109return false;110111// Do some basic sanity checks on the directories that we don't activate112// the module when it's clear that it's not usable.113const std::vector<std::string> files_to_check = {114// * Check that the C library contains at least one random C standard115// library header.116MakePath(m_c_inc.Get(), "stdio.h"),117// * Without a libc++ modulemap file we can't have a 'std' module that118// could be imported.119MakePath(m_std_inc.Get(), "module.modulemap"),120// * Check for a random libc++ header (vector in this case) that has to121// exist in a working libc++ setup.122MakePath(m_std_inc.Get(), "vector"),123};124125for (llvm::StringRef file_to_check : files_to_check) {126if (!FileSystem::Instance().Exists(file_to_check))127return false;128}129130return true;131}132133CppModuleConfiguration::CppModuleConfiguration(134const FileSpecList &support_files, const llvm::Triple &triple) {135// Analyze all files we were given to build the configuration.136bool error = !llvm::all_of(support_files, [&](auto &file) {137return CppModuleConfiguration::analyzeFile(file, triple);138});139// If we have a valid configuration at this point, set the140// include directories and module list that should be used.141if (!error && hasValidConfig()) {142// Calculate the resource directory for LLDB.143llvm::SmallString<256> resource_dir;144llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),145"include");146m_resource_inc = std::string(resource_dir.str());147148// This order matches the way Clang orders these directories.149m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,150m_c_inc.Get().str()};151if (m_c_target_inc.Valid())152m_include_dirs.push_back(m_c_target_inc.Get().str());153if (m_std_target_inc.Valid())154m_include_dirs.push_back(m_std_target_inc.Get().str());155m_imported_modules = {"std"};156}157}158159160