Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CppModuleConfiguration.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "llvm/ADT/Triple.h"
14
15
using namespace lldb_private;
16
17
211k
bool CppModuleConfiguration::SetOncePath::TrySet(llvm::StringRef path) {
18
  // Setting for the first time always works.
19
211k
  if (m_first) {
20
2.42k
    m_path = path.str();
21
2.42k
    m_valid = true;
22
2.42k
    m_first = false;
23
2.42k
    return true;
24
2.42k
  }
25
  // Changing the path to the same value is fine.
26
208k
  if (m_path == path)
27
208k
    return true;
28
29
  // Changing the path after it was already set is not allowed.
30
0
  m_valid = false;
31
0
  return false;
32
208k
}
33
34
static llvm::SmallVector<std::string, 2>
35
230k
getTargetIncludePaths(const llvm::Triple &triple) {
36
230k
  llvm::SmallVector<std::string, 2> paths;
37
230k
  if (!triple.str().empty()) {
38
230k
    paths.push_back("/usr/include/" + triple.str());
39
230k
    if (!triple.getArchName().empty() ||
40
230k
        
triple.getOSAndEnvironmentName().empty()0
)
41
230k
      paths.push_back(("/usr/include/" + triple.getArchName() + "-" +
42
230k
                       triple.getOSAndEnvironmentName())
43
230k
                          .str());
44
230k
  }
45
230k
  return paths;
46
230k
}
47
48
/// Returns the include path matching the given pattern for the given file
49
/// path (or None if the path doesn't match the pattern).
50
static llvm::Optional<llvm::StringRef>
51
690k
guessIncludePath(llvm::StringRef path_to_file, llvm::StringRef pattern) {
52
690k
  if (pattern.empty())
53
0
    return llvm::NoneType();
54
690k
  size_t pos = path_to_file.find(pattern);
55
690k
  if (pos == llvm::StringRef::npos)
56
564k
    return llvm::NoneType();
57
58
126k
  return path_to_file.substr(0, pos + pattern.size());
59
690k
}
60
61
bool CppModuleConfiguration::analyzeFile(const FileSpec &f,
62
272k
                                         const llvm::Triple &triple) {
63
272k
  using namespace llvm::sys::path;
64
  // Convert to slashes to make following operations simpler.
65
272k
  std::string dir_buffer = convert_to_slash(f.GetDirectory().GetStringRef());
66
272k
  llvm::StringRef posix_dir(dir_buffer);
67
68
  // Check for /c++/vX/ that is used by libc++.
69
272k
  static 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't
72
  // need to be specified in the header search.
73
272k
  if (libcpp_regex.match(f.GetPath()) &&
74
272k
      
parent_path(posix_dir, Style::posix).endswith("c++")140k
) {
75
42.6k
    if (!m_std_inc.TrySet(posix_dir))
76
0
      return false;
77
42.6k
    if (triple.str().empty())
78
17
      return true;
79
80
42.5k
    posix_dir.consume_back("c++/v1");
81
    // Check if this is a target-specific libc++ include directory.
82
42.5k
    return m_std_target_inc.TrySet(
83
42.5k
        (posix_dir + triple.str() + "/c++/v1").str());
84
42.6k
  }
85
86
230k
  llvm::Optional<llvm::StringRef> inc_path;
87
  // Target specific paths contains /usr/include, so we check them first
88
460k
  for (auto &path : getTargetIncludePaths(triple)) {
89
460k
    if ((inc_path = guessIncludePath(posix_dir, path)))
90
1
      return m_c_target_inc.TrySet(*inc_path);
91
460k
  }
92
230k
  if ((inc_path = guessIncludePath(posix_dir, "/usr/include")))
93
126k
    return m_c_inc.TrySet(*inc_path);
94
95
  // File wasn't interesting, continue analyzing.
96
104k
  return true;
97
230k
}
98
99
/// Utility function for just appending two paths.
100
2.43k
static std::string MakePath(llvm::StringRef lhs, llvm::StringRef rhs) {
101
2.43k
  llvm::SmallString<256> result(lhs);
102
2.43k
  llvm::sys::path::append(result, rhs);
103
2.43k
  return std::string(result);
104
2.43k
}
105
106
827
bool CppModuleConfiguration::hasValidConfig() {
107
  // We need to have a C and C++ include dir for a valid configuration.
108
827
  if (!m_c_inc.Valid() || 
!m_std_inc.Valid()814
)
109
17
    return false;
110
111
  // Do some basic sanity checks on the directories that we don't activate
112
  // the module when it's clear that it's not usable.
113
810
  const std::vector<std::string> files_to_check = {
114
      // * Check that the C library contains at least one random C standard
115
      //   library header.
116
810
      MakePath(m_c_inc.Get(), "stdio.h"),
117
      // * Without a libc++ modulemap file we can't have a 'std' module that
118
      //   could be imported.
119
810
      MakePath(m_std_inc.Get(), "module.modulemap"),
120
      // * Check for a random libc++ header (vector in this case) that has to
121
      //   exist in a working libc++ setup.
122
810
      MakePath(m_std_inc.Get(), "vector"),
123
810
  };
124
125
2.40k
  for (llvm::StringRef file_to_check : files_to_check) {
126
2.40k
    if (!FileSystem::Instance().Exists(file_to_check))
127
12
      return false;
128
2.40k
  }
129
130
798
  return true;
131
810
}
132
133
CppModuleConfiguration::CppModuleConfiguration(
134
827
    const FileSpecList &support_files, const llvm::Triple &triple) {
135
  // Analyze all files we were given to build the configuration.
136
827
  bool error = !llvm::all_of(support_files,
137
827
                             std::bind(&CppModuleConfiguration::analyzeFile,
138
827
                                       this, std::placeholders::_1, triple));
139
  // If we have a valid configuration at this point, set the
140
  // include directories and module list that should be used.
141
827
  if (!error && hasValidConfig()) {
142
    // Calculate the resource directory for LLDB.
143
798
    llvm::SmallString<256> resource_dir;
144
798
    llvm::sys::path::append(resource_dir, GetClangResourceDir().GetPath(),
145
798
                            "include");
146
798
    m_resource_inc = std::string(resource_dir.str());
147
148
    // This order matches the way Clang orders these directories.
149
798
    m_include_dirs = {m_std_inc.Get().str(), m_resource_inc,
150
798
                      m_c_inc.Get().str()};
151
798
    if (m_c_target_inc.Valid())
152
1
      m_include_dirs.push_back(m_c_target_inc.Get().str());
153
798
    if (m_std_target_inc.Valid())
154
791
      m_include_dirs.push_back(m_std_target_inc.Get().str());
155
798
    m_imported_modules = {"std"};
156
798
  }
157
827
}