LLDB  mainline
CppModuleConfiguration.cpp
Go to the documentation of this file.
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 
10 
11 #include "ClangHost.h"
12 #include "lldb/Host/FileSystem.h"
13 
14 using namespace lldb_private;
15 
16 bool 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 
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 the path is in the libc++ include directory use it as the found libc++
42  // path. Ignore subdirectories such as /c++/v1/experimental as those don't
43  // need to be specified in the header search.
44  if (libcpp_regex.match(f.GetPath()) &&
45  parent_path(posix_dir, Style::posix).endswith("c++")) {
46  return m_std_inc.TrySet(posix_dir);
47  }
48 
49  // Check for /usr/include. On Linux this might be /usr/include/bits, so
50  // we should remove that '/bits' suffix to get the actual include directory.
51  if (posix_dir.endswith("/usr/include/bits"))
52  posix_dir.consume_back("/bits");
53  if (posix_dir.endswith("/usr/include"))
54  return m_c_inc.TrySet(posix_dir);
55 
56  // File wasn't interesting, continue analyzing.
57  return true;
58 }
59 
60 /// Utility function for just appending two paths.
61 static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
62  llvm::SmallString<256> result(lhs);
63  llvm::sys::path::append(result, rhs);
64  return std::string(result);
65 }
66 
68  // We need to have a C and C++ include dir for a valid configuration.
69  if (!m_c_inc.Valid() || !m_std_inc.Valid())
70  return false;
71 
72  // Do some basic sanity checks on the directories that we don't activate
73  // the module when it's clear that it's not usable.
74  const std::vector<std::string> files_to_check = {
75  // * Check that the C library contains at least one random C standard
76  // library header.
77  MakePath(m_c_inc.Get(), "stdio.h"),
78  // * Without a libc++ modulemap file we can't have a 'std' module that
79  // could be imported.
80  MakePath(m_std_inc.Get(), "module.modulemap"),
81  // * Check for a random libc++ header (vector in this case) that has to
82  // exist in a working libc++ setup.
83  MakePath(m_std_inc.Get(), "vector"),
84  };
85 
86  for (llvm::StringRef file_to_check : files_to_check) {
87  if (!FileSystem::Instance().Exists(file_to_check))
88  return false;
89  }
90 
91  return true;
92 }
93 
95  const FileSpecList &support_files) {
96  // Analyze all files we were given to build the configuration.
97  bool error = !llvm::all_of(support_files,
99  this, std::placeholders::_1));
100  // If we have a valid configuration at this point, set the
101  // include directories and module list that should be used.
102  if (!error && hasValidConfig()) {
103  // Calculate the resource directory for LLDB.
104  llvm::SmallString<256> resource_dir;
105  llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
106  "include");
107  m_resource_inc = std::string(resource_dir.str());
108 
109  // This order matches the way Clang orders these directories.
111  m_c_inc.Get().str()};
112  m_imported_modules = {"std"};
113  }
114 }
lldb_private::CppModuleConfiguration::m_c_inc
SetOncePath m_c_inc
If valid, the include path to the C library (e.g. /usr/include).
Definition: CppModuleConfiguration.h:46
lldb_private::CppModuleConfiguration::analyzeFile
bool analyzeFile(const FileSpec &f)
Analyze a given source file to build the current configuration.
Definition: CppModuleConfiguration.cpp:33
lldb_private::CppModuleConfiguration::SetOncePath::m_first
bool m_first
True iff this path hasn't been set yet.
Definition: CppModuleConfiguration.h:28
FileSystem.h
lldb_private::CppModuleConfiguration::m_include_dirs
std::vector< std::string > m_include_dirs
Definition: CppModuleConfiguration.h:50
lldb_private::CppModuleConfiguration::SetOncePath::m_path
std::string m_path
Definition: CppModuleConfiguration.h:25
lldb_private::CppModuleConfiguration::SetOncePath::Get
llvm::StringRef Get() const
Return the path if there is one.
Definition: CppModuleConfiguration.h:35
lldb_private::CppModuleConfiguration::SetOncePath::TrySet
LLVM_NODISCARD bool TrySet(llvm::StringRef path)
Try setting the path.
Definition: CppModuleConfiguration.cpp:16
lldb_private::CppModuleConfiguration::SetOncePath::Valid
bool Valid() const
Returns true iff this path was set exactly once so far.
Definition: CppModuleConfiguration.h:40
CppModuleConfiguration.h
lldb_private::GetClangResourceDir
FileSpec GetClangResourceDir()
Definition: ClangHost.cpp:153
ClangHost.h
lldb_private::CppModuleConfiguration::SetOncePath::m_valid
bool m_valid
Definition: CppModuleConfiguration.h:26
lldb_private::CppModuleConfiguration::m_std_inc
SetOncePath m_std_inc
If valid, the include path used for the std module.
Definition: CppModuleConfiguration.h:44
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::CppModuleConfiguration::m_resource_inc
std::string m_resource_inc
The Clang resource include path for this configuration.
Definition: CppModuleConfiguration.h:48
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
lldb_private::CppModuleConfiguration::m_imported_modules
std::vector< std::string > m_imported_modules
Definition: CppModuleConfiguration.h:51
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::CppModuleConfiguration::CppModuleConfiguration
CppModuleConfiguration()
Creates an empty and invalid configuration.
Definition: CppModuleConfiguration.h:62
MakePath
static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs)
Utility function for just appending two paths.
Definition: CppModuleConfiguration.cpp:61
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::FileSpec::GetDirectory
ConstString & GetDirectory()
Directory string get accessor.
Definition: FileSpec.cpp:335
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb_private::CppModuleConfiguration::hasValidConfig
bool hasValidConfig()
Returns true iff this is a valid configuration that can be used to load and compile modules.
Definition: CppModuleConfiguration.cpp:67