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