]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Frontend/ModuleDependencyCollector.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[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   // Explicitly set case sensitivity for the YAML writer. For that, find out
160   // the sensitivity at the path where the headers all collected to.
161   VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
162
163   // Do not rely on real path names when executing the crash reproducer scripts
164   // since we only want to actually use the files we have on the VFS cache.
165   VFSWriter.setUseExternalNames(false);
166
167   std::error_code EC;
168   SmallString<256> YAMLPath = VFSDir;
169   llvm::sys::path::append(YAMLPath, "vfs.yaml");
170   llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
171   if (EC) {
172     HasErrors = true;
173     return;
174   }
175   VFSWriter.write(OS);
176 }
177
178 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
179                                             SmallVectorImpl<char> &Result) {
180   using namespace llvm::sys;
181   SmallString<256> RealPath;
182   StringRef FileName = path::filename(SrcPath);
183   std::string Dir = path::parent_path(SrcPath).str();
184   auto DirWithSymLink = SymLinkMap.find(Dir);
185
186   // Use real_path to fix any symbolic link component present in a path.
187   // Computing the real path is expensive, cache the search through the
188   // parent path directory.
189   if (DirWithSymLink == SymLinkMap.end()) {
190     if (!real_path(Dir, RealPath))
191       return false;
192     SymLinkMap[Dir] = RealPath.str();
193   } else {
194     RealPath = DirWithSymLink->second;
195   }
196
197   path::append(RealPath, FileName);
198   Result.swap(RealPath);
199   return true;
200 }
201
202 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
203                                                       StringRef Dst) {
204   using namespace llvm::sys;
205
206   // We need an absolute src path to append to the root.
207   SmallString<256> AbsoluteSrc = Src;
208   fs::make_absolute(AbsoluteSrc);
209   // Canonicalize src to a native path to avoid mixed separator styles.
210   path::native(AbsoluteSrc);
211   // Remove redundant leading "./" pieces and consecutive separators.
212   AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
213
214   // Canonicalize the source path by removing "..", "." components.
215   SmallString<256> VirtualPath = AbsoluteSrc;
216   path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
217
218   // If a ".." component is present after a symlink component, remove_dots may
219   // lead to the wrong real destination path. Let the source be canonicalized
220   // like that but make sure we always use the real path for the destination.
221   SmallString<256> CopyFrom;
222   if (!getRealPath(AbsoluteSrc, CopyFrom))
223     CopyFrom = VirtualPath;
224   SmallString<256> CacheDst = getDest();
225
226   if (Dst.empty()) {
227     // The common case is to map the virtual path to the same path inside the
228     // cache.
229     path::append(CacheDst, path::relative_path(CopyFrom));
230   } else {
231     // When collecting entries from input vfsoverlays, copy the external
232     // contents into the cache but still map from the source.
233     if (!fs::exists(Dst))
234       return std::error_code();
235     path::append(CacheDst, Dst);
236     CopyFrom = Dst;
237   }
238
239   // Copy the file into place.
240   if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
241                                                   /*IgnoreExisting=*/true))
242     return EC;
243   if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
244     return EC;
245
246   // Always map a canonical src path to its real path into the YAML, by doing
247   // this we map different virtual src paths to the same entry in the VFS
248   // overlay, which is a way to emulate symlink inside the VFS; this is also
249   // needed for correctness, not doing that can lead to module redefinition
250   // errors.
251   addFileMapping(VirtualPath, CacheDst);
252   return std::error_code();
253 }
254
255 void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
256   if (insertSeen(Filename))
257     if (copyToRoot(Filename, FileDst))
258       HasErrors = true;
259 }