Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/ModuleDependencyCollector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
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
// Collect the dependencies of a set of modules.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/CharInfo.h"
14
#include "clang/Frontend/Utils.h"
15
#include "clang/Lex/Preprocessor.h"
16
#include "clang/Serialization/ASTReader.h"
17
#include "llvm/ADT/iterator_range.h"
18
#include "llvm/Config/llvm-config.h"
19
#include "llvm/Support/FileSystem.h"
20
#include "llvm/Support/Path.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
using namespace clang;
24
25
namespace {
26
/// Private implementations for ModuleDependencyCollector
27
class ModuleDependencyListener : public ASTReaderListener {
28
  ModuleDependencyCollector &Collector;
29
public:
30
  ModuleDependencyListener(ModuleDependencyCollector &Collector)
31
34
      : Collector(Collector) {}
32
113
  bool needsInputFileVisitation() override { return true; }
33
69
  bool needsSystemInputFileVisitation() override { return true; }
34
  bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
35
90
                      bool IsExplicitModule) override {
36
90
    Collector.addFile(Filename);
37
90
    return true;
38
90
  }
39
};
40
41
struct ModuleDependencyPPCallbacks : public PPCallbacks {
42
  ModuleDependencyCollector &Collector;
43
  SourceManager &SM;
44
  ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
45
                              SourceManager &SM)
46
35
      : Collector(Collector), SM(SM) {}
47
48
  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
49
                          StringRef FileName, bool IsAngled,
50
                          CharSourceRange FilenameRange, const FileEntry *File,
51
                          StringRef SearchPath, StringRef RelativePath,
52
                          const Module *Imported,
53
68
                          SrcMgr::CharacteristicKind FileType) override {
54
68
    if (!File)
55
0
      return;
56
68
    Collector.addFile(File->getName());
57
68
  }
58
};
59
60
struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
61
  ModuleDependencyCollector &Collector;
62
  ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
63
35
      : Collector(Collector) {}
64
65
709
  void moduleMapAddHeader(StringRef HeaderPath) override {
66
709
    if (llvm::sys::path::is_absolute(HeaderPath))
67
703
      Collector.addFile(HeaderPath);
68
709
  }
69
  void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
70
21
                                  const FileEntry *Header) override {
71
21
    StringRef HeaderFilename = Header->getName();
72
21
    moduleMapAddHeader(HeaderFilename);
73
21
    // The FileManager can find and cache the symbolic link for a framework
74
21
    // header before its real path, this means a module can have some of its
75
21
    // headers to use other paths. Although this is usually not a problem, it's
76
21
    // inconsistent, and not collecting the original path header leads to
77
21
    // umbrella clashes while rebuilding modules in the crash reproducer. For
78
21
    // example:
79
21
    //    ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
80
21
    // instead of:
81
21
    //    ImageIO.framework/ImageIO.h
82
21
    //
83
21
    // FIXME: this shouldn't be necessary once we have FileName instances
84
21
    // around instead of FileEntry ones. For now, make sure we collect all
85
21
    // that we need for the reproducer to work correctly.
86
21
    StringRef UmbreallDirFromHeader =
87
21
        llvm::sys::path::parent_path(HeaderFilename);
88
21
    StringRef UmbrellaDir = Header->getDir()->getName();
89
21
    if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
90
4
      SmallString<128> AltHeaderFilename;
91
4
      llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
92
4
                              llvm::sys::path::filename(HeaderFilename));
93
4
      if (FileMgr->getFile(AltHeaderFilename))
94
4
        moduleMapAddHeader(AltHeaderFilename);
95
4
    }
96
21
  }
97
};
98
99
}
100
101
34
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
102
34
  R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
103
34
}
104
105
35
void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
106
35
  PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
107
35
      *this, PP.getSourceManager()));
108
35
  PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
109
35
      llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
110
35
}
111
112
16
static bool isCaseSensitivePath(StringRef Path) {
113
16
  SmallString<256> TmpDest = Path, UpperDest, RealDest;
114
16
  // Remove component traversals, links, etc.
115
16
  if (llvm::sys::fs::real_path(Path, TmpDest))
116
0
    return true; // Current default value in vfs.yaml
117
16
  Path = TmpDest;
118
16
119
16
  // Change path to all upper case and ask for its real path, if the latter
120
16
  // exists and is equal to Path, it's not case sensitive. Default to case
121
16
  // sensitive in the absence of realpath, since this is what the VFSWriter
122
16
  // already expects when sensitivity isn't setup.
123
16
  for (auto &C : Path)
124
2.74k
    UpperDest.push_back(toUppercase(C));
125
16
  if (!llvm::sys::fs::real_path(UpperDest, RealDest) && 
Path.equals(RealDest)0
)
126
0
    return false;
127
16
  return true;
128
16
}
129
130
16
void ModuleDependencyCollector::writeFileMap() {
131
16
  if (Seen.empty())
132
0
    return;
133
16
134
16
  StringRef VFSDir = getDest();
135
16
136
16
  // Default to use relative overlay directories in the VFS yaml file. This
137
16
  // allows crash reproducer scripts to work across machines.
138
16
  VFSWriter.setOverlayDir(VFSDir);
139
16
140
16
  // Explicitly set case sensitivity for the YAML writer. For that, find out
141
16
  // the sensitivity at the path where the headers all collected to.
142
16
  VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
143
16
144
16
  // Do not rely on real path names when executing the crash reproducer scripts
145
16
  // since we only want to actually use the files we have on the VFS cache.
146
16
  VFSWriter.setUseExternalNames(false);
147
16
148
16
  std::error_code EC;
149
16
  SmallString<256> YAMLPath = VFSDir;
150
16
  llvm::sys::path::append(YAMLPath, "vfs.yaml");
151
16
  llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
152
16
  if (EC) {
153
0
    HasErrors = true;
154
0
    return;
155
0
  }
156
16
  VFSWriter.write(OS);
157
16
}
158
159
bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
160
527
                                            SmallVectorImpl<char> &Result) {
161
527
  using namespace llvm::sys;
162
527
  SmallString<256> RealPath;
163
527
  StringRef FileName = path::filename(SrcPath);
164
527
  std::string Dir = path::parent_path(SrcPath).str();
165
527
  auto DirWithSymLink = SymLinkMap.find(Dir);
166
527
167
527
  // Use real_path to fix any symbolic link component present in a path.
168
527
  // Computing the real path is expensive, cache the search through the
169
527
  // parent path directory.
170
527
  if (DirWithSymLink == SymLinkMap.end()) {
171
47
    if (llvm::sys::fs::real_path(Dir, RealPath))
172
1
      return false;
173
46
    SymLinkMap[Dir] = RealPath.str();
174
480
  } else {
175
480
    RealPath = DirWithSymLink->second;
176
480
  }
177
527
178
527
  path::append(RealPath, FileName);
179
526
  Result.swap(RealPath);
180
526
  return true;
181
527
}
182
183
std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
184
527
                                                      StringRef Dst) {
185
527
  using namespace llvm::sys;
186
527
187
527
  // We need an absolute src path to append to the root.
188
527
  SmallString<256> AbsoluteSrc = Src;
189
527
  fs::make_absolute(AbsoluteSrc);
190
527
  // Canonicalize src to a native path to avoid mixed separator styles.
191
527
  path::native(AbsoluteSrc);
192
527
  // Remove redundant leading "./" pieces and consecutive separators.
193
527
  AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
194
527
195
527
  // Canonicalize the source path by removing "..", "." components.
196
527
  SmallString<256> VirtualPath = AbsoluteSrc;
197
527
  path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
198
527
199
527
  // If a ".." component is present after a symlink component, remove_dots may
200
527
  // lead to the wrong real destination path. Let the source be canonicalized
201
527
  // like that but make sure we always use the real path for the destination.
202
527
  SmallString<256> CopyFrom;
203
527
  if (!getRealPath(AbsoluteSrc, CopyFrom))
204
1
    CopyFrom = VirtualPath;
205
527
  SmallString<256> CacheDst = getDest();
206
527
207
527
  if (Dst.empty()) {
208
526
    // The common case is to map the virtual path to the same path inside the
209
526
    // cache.
210
526
    path::append(CacheDst, path::relative_path(CopyFrom));
211
526
  } else {
212
1
    // When collecting entries from input vfsoverlays, copy the external
213
1
    // contents into the cache but still map from the source.
214
1
    if (!fs::exists(Dst))
215
0
      return std::error_code();
216
1
    path::append(CacheDst, Dst);
217
1
    CopyFrom = Dst;
218
1
  }
219
527
220
527
  // Copy the file into place.
221
527
  if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
222
0
                                                  /*IgnoreExisting=*/true))
223
0
    return EC;
224
527
  if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
225
4
    return EC;
226
523
227
523
  // Always map a canonical src path to its real path into the YAML, by doing
228
523
  // this we map different virtual src paths to the same entry in the VFS
229
523
  // overlay, which is a way to emulate symlink inside the VFS; this is also
230
523
  // needed for correctness, not doing that can lead to module redefinition
231
523
  // errors.
232
523
  addFileMapping(VirtualPath, CacheDst);
233
523
  return std::error_code();
234
523
}
235
236
865
void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
237
865
  if (insertSeen(Filename))
238
527
    if (copyToRoot(Filename, FileDst))
239
4
      HasErrors = true;
240
865
}