1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
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 // This code generates dependency files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Frontend/DependencyOutputOptions.h"
18 #include "clang/Frontend/FrontendDiagnostic.h"
19 #include "clang/Lex/DirectoryLookup.h"
20 #include "clang/Lex/LexDiagnostic.h"
21 #include "clang/Lex/ModuleMap.h"
22 #include "clang/Lex/PPCallbacks.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Serialization/ASTReader.h"
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 using namespace clang;
32 
33 namespace {
34 struct DepCollectorPPCallbacks : public PPCallbacks {
35   DependencyCollector &DepCollector;
36   SourceManager &SM;
37   DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
38       : DepCollector(L), SM(SM) { }
39 
40   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
41                    SrcMgr::CharacteristicKind FileType,
42                    FileID PrevFID) override {
43     if (Reason != PPCallbacks::EnterFile)
44       return;
45 
46     // Dependency generation really does want to go all the way to the
47     // file entry for a source location to find out what is depended on.
48     // We do not want #line markers to affect dependency generation!
49     const FileEntry *FE =
50         SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
51     if (!FE)
52       return;
53 
54     StringRef Filename =
55         llvm::sys::path::remove_leading_dotslash(FE->getName());
56 
57     DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
58                                     isSystem(FileType),
59                                     /*IsModuleFile*/false, /*IsMissing*/false);
60   }
61 
62   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
63                           StringRef FileName, bool IsAngled,
64                           CharSourceRange FilenameRange, const FileEntry *File,
65                           StringRef SearchPath, StringRef RelativePath,
66                           const Module *Imported,
67                           SrcMgr::CharacteristicKind FileType) override {
68     if (!File)
69       DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
70                                      /*IsSystem*/false, /*IsModuleFile*/false,
71                                      /*IsMissing*/true);
72     // Files that actually exist are handled by FileChanged.
73   }
74 
75   void EndOfMainFile() override {
76     DepCollector.finishedMainFile();
77   }
78 };
79 
80 struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
81   DependencyCollector &DepCollector;
82   DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
83 
84   void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
85                          bool IsSystem) override {
86     StringRef Filename = Entry.getName();
87     DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
88                                     /*IsSystem*/IsSystem,
89                                     /*IsModuleFile*/false,
90                                     /*IsMissing*/false);
91   }
92 };
93 
94 struct DepCollectorASTListener : public ASTReaderListener {
95   DependencyCollector &DepCollector;
96   DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
97   bool needsInputFileVisitation() override { return true; }
98   bool needsSystemInputFileVisitation() override {
99     return DepCollector.needSystemDependencies();
100   }
101   void visitModuleFile(StringRef Filename,
102                        serialization::ModuleKind Kind) override {
103     DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
104                                    /*IsSystem*/false, /*IsModuleFile*/true,
105                                    /*IsMissing*/false);
106   }
107   bool visitInputFile(StringRef Filename, bool IsSystem,
108                       bool IsOverridden, bool IsExplicitModule) override {
109     if (IsOverridden || IsExplicitModule)
110       return true;
111 
112     DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
113                                    /*IsModuleFile*/false, /*IsMissing*/false);
114     return true;
115   }
116 };
117 } // end anonymous namespace
118 
119 void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
120                                             bool IsSystem, bool IsModuleFile,
121                                             bool IsMissing) {
122   if (Seen.insert(Filename).second &&
123       sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
124     Dependencies.push_back(Filename);
125 }
126 
127 static bool isSpecialFilename(StringRef Filename) {
128   return llvm::StringSwitch<bool>(Filename)
129       .Case("<built-in>", true)
130       .Case("<stdin>", true)
131       .Default(false);
132 }
133 
134 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
135                                        bool IsSystem, bool IsModuleFile,
136                                        bool IsMissing) {
137   return !isSpecialFilename(Filename) &&
138          (needSystemDependencies() || !IsSystem);
139 }
140 
141 DependencyCollector::~DependencyCollector() { }
142 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
143   PP.addPPCallbacks(
144       llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
145   PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
146       llvm::make_unique<DepCollectorMMCallbacks>(*this));
147 }
148 void DependencyCollector::attachToASTReader(ASTReader &R) {
149   R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
150 }
151 
152 namespace {
153 /// Private implementation for DependencyFileGenerator
154 class DFGImpl : public PPCallbacks {
155   std::vector<std::string> Files;
156   llvm::StringSet<> FilesSet;
157   const Preprocessor *PP;
158   std::string OutputFile;
159   std::vector<std::string> Targets;
160   bool IncludeSystemHeaders;
161   bool PhonyTarget;
162   bool AddMissingHeaderDeps;
163   bool SeenMissingHeader;
164   bool IncludeModuleFiles;
165   DependencyOutputFormat OutputFormat;
166 
167 private:
168   bool FileMatchesDepCriteria(const char *Filename,
169                               SrcMgr::CharacteristicKind FileType);
170   void OutputDependencyFile();
171 
172 public:
173   DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
174     : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
175       IncludeSystemHeaders(Opts.IncludeSystemHeaders),
176       PhonyTarget(Opts.UsePhonyTargets),
177       AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
178       SeenMissingHeader(false),
179       IncludeModuleFiles(Opts.IncludeModuleFiles),
180       OutputFormat(Opts.OutputFormat) {
181     for (const auto &ExtraDep : Opts.ExtraDeps) {
182       AddFilename(ExtraDep);
183     }
184   }
185 
186   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
187                    SrcMgr::CharacteristicKind FileType,
188                    FileID PrevFID) override;
189 
190   void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
191                    SrcMgr::CharacteristicKind FileType) override;
192 
193   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
194                           StringRef FileName, bool IsAngled,
195                           CharSourceRange FilenameRange, const FileEntry *File,
196                           StringRef SearchPath, StringRef RelativePath,
197                           const Module *Imported,
198                           SrcMgr::CharacteristicKind FileType) override;
199 
200   void EndOfMainFile() override {
201     OutputDependencyFile();
202   }
203 
204   void AddFilename(StringRef Filename);
205   bool includeSystemHeaders() const { return IncludeSystemHeaders; }
206   bool includeModuleFiles() const { return IncludeModuleFiles; }
207 };
208 
209 class DFGMMCallback : public ModuleMapCallbacks {
210   DFGImpl &Parent;
211 public:
212   DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
213   void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
214                          bool IsSystem) override {
215     if (!IsSystem || Parent.includeSystemHeaders())
216       Parent.AddFilename(Entry.getName());
217   }
218 };
219 
220 class DFGASTReaderListener : public ASTReaderListener {
221   DFGImpl &Parent;
222 public:
223   DFGASTReaderListener(DFGImpl &Parent)
224   : Parent(Parent) { }
225   bool needsInputFileVisitation() override { return true; }
226   bool needsSystemInputFileVisitation() override {
227     return Parent.includeSystemHeaders();
228   }
229   void visitModuleFile(StringRef Filename,
230                        serialization::ModuleKind Kind) override;
231   bool visitInputFile(StringRef Filename, bool isSystem,
232                       bool isOverridden, bool isExplicitModule) override;
233 };
234 }
235 
236 DependencyFileGenerator::DependencyFileGenerator(void *Impl)
237 : Impl(Impl) { }
238 
239 DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
240     clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
241 
242   if (Opts.Targets.empty()) {
243     PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
244     return nullptr;
245   }
246 
247   // Disable the "file not found" diagnostic if the -MG option was given.
248   if (Opts.AddMissingHeaderDeps)
249     PP.SetSuppressIncludeNotFoundError(true);
250 
251   DFGImpl *Callback = new DFGImpl(&PP, Opts);
252   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
253   PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
254       llvm::make_unique<DFGMMCallback>(*Callback));
255   return new DependencyFileGenerator(Callback);
256 }
257 
258 void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
259   DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
260   assert(I && "missing implementation");
261   R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
262 }
263 
264 /// FileMatchesDepCriteria - Determine whether the given Filename should be
265 /// considered as a dependency.
266 bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
267                                      SrcMgr::CharacteristicKind FileType) {
268   if (isSpecialFilename(Filename))
269     return false;
270 
271   if (IncludeSystemHeaders)
272     return true;
273 
274   return !isSystem(FileType);
275 }
276 
277 void DFGImpl::FileChanged(SourceLocation Loc,
278                           FileChangeReason Reason,
279                           SrcMgr::CharacteristicKind FileType,
280                           FileID PrevFID) {
281   if (Reason != PPCallbacks::EnterFile)
282     return;
283 
284   // Dependency generation really does want to go all the way to the
285   // file entry for a source location to find out what is depended on.
286   // We do not want #line markers to affect dependency generation!
287   SourceManager &SM = PP->getSourceManager();
288 
289   const FileEntry *FE =
290     SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
291   if (!FE) return;
292 
293   StringRef Filename = FE->getName();
294   if (!FileMatchesDepCriteria(Filename.data(), FileType))
295     return;
296 
297   AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
298 }
299 
300 void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
301                           const Token &FilenameTok,
302                           SrcMgr::CharacteristicKind FileType) {
303   StringRef Filename = SkippedFile.getName();
304   if (!FileMatchesDepCriteria(Filename.data(), FileType))
305     return;
306 
307   AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
308 }
309 
310 void DFGImpl::InclusionDirective(SourceLocation HashLoc,
311                                  const Token &IncludeTok,
312                                  StringRef FileName,
313                                  bool IsAngled,
314                                  CharSourceRange FilenameRange,
315                                  const FileEntry *File,
316                                  StringRef SearchPath,
317                                  StringRef RelativePath,
318                                  const Module *Imported,
319                                  SrcMgr::CharacteristicKind FileType) {
320   if (!File) {
321     if (AddMissingHeaderDeps)
322       AddFilename(FileName);
323     else
324       SeenMissingHeader = true;
325   }
326 }
327 
328 void DFGImpl::AddFilename(StringRef Filename) {
329   if (FilesSet.insert(Filename).second)
330     Files.push_back(Filename);
331 }
332 
333 /// Print the filename, with escaping or quoting that accommodates the three
334 /// most likely tools that use dependency files: GNU Make, BSD Make, and
335 /// NMake/Jom.
336 ///
337 /// BSD Make is the simplest case: It does no escaping at all.  This means
338 /// characters that are normally delimiters, i.e. space and # (the comment
339 /// character) simply aren't supported in filenames.
340 ///
341 /// GNU Make does allow space and # in filenames, but to avoid being treated
342 /// as a delimiter or comment, these must be escaped with a backslash. Because
343 /// backslash is itself the escape character, if a backslash appears in a
344 /// filename, it should be escaped as well.  (As a special case, $ is escaped
345 /// as $$, which is the normal Make way to handle the $ character.)
346 /// For compatibility with BSD Make and historical practice, if GNU Make
347 /// un-escapes characters in a filename but doesn't find a match, it will
348 /// retry with the unmodified original string.
349 ///
350 /// GCC tries to accommodate both Make formats by escaping any space or #
351 /// characters in the original filename, but not escaping backslashes.  The
352 /// apparent intent is so that filenames with backslashes will be handled
353 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
354 /// unmodified original string; filenames with # or space characters aren't
355 /// supported by BSD Make at all, but will be handled correctly by GNU Make
356 /// due to the escaping.
357 ///
358 /// A corner case that GCC gets only partly right is when the original filename
359 /// has a backslash immediately followed by space or #.  GNU Make would expect
360 /// this backslash to be escaped; however GCC escapes the original backslash
361 /// only when followed by space, not #.  It will therefore take a dependency
362 /// from a directive such as
363 ///     #include "a\ b\#c.h"
364 /// and emit it as
365 ///     a\\\ b\\#c.h
366 /// which GNU Make will interpret as
367 ///     a\ b\
368 /// followed by a comment. Failing to find this file, it will fall back to the
369 /// original string, which probably doesn't exist either; in any case it won't
370 /// find
371 ///     a\ b\#c.h
372 /// which is the actual filename specified by the include directive.
373 ///
374 /// Clang does what GCC does, rather than what GNU Make expects.
375 ///
376 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
377 /// double-quotes to avoid misinterpreting them; see
378 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
379 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
380 /// for Windows file-naming info.
381 static void PrintFilename(raw_ostream &OS, StringRef Filename,
382                           DependencyOutputFormat OutputFormat) {
383   if (OutputFormat == DependencyOutputFormat::NMake) {
384     // Add quotes if needed. These are the characters listed as "special" to
385     // NMake, that are legal in a Windows filespec, and that could cause
386     // misinterpretation of the dependency string.
387     if (Filename.find_first_of(" #${}^!") != StringRef::npos)
388       OS << '\"' << Filename << '\"';
389     else
390       OS << Filename;
391     return;
392   }
393   assert(OutputFormat == DependencyOutputFormat::Make);
394   for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
395     if (Filename[i] == '#') // Handle '#' the broken gcc way.
396       OS << '\\';
397     else if (Filename[i] == ' ') { // Handle space correctly.
398       OS << '\\';
399       unsigned j = i;
400       while (j > 0 && Filename[--j] == '\\')
401         OS << '\\';
402     } else if (Filename[i] == '$') // $ is escaped by $$.
403       OS << '$';
404     OS << Filename[i];
405   }
406 }
407 
408 void DFGImpl::OutputDependencyFile() {
409   if (SeenMissingHeader) {
410     llvm::sys::fs::remove(OutputFile);
411     return;
412   }
413 
414   std::error_code EC;
415   llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
416   if (EC) {
417     PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
418                                                             << EC.message();
419     return;
420   }
421 
422   // Write out the dependency targets, trying to avoid overly long
423   // lines when possible. We try our best to emit exactly the same
424   // dependency file as GCC (4.2), assuming the included files are the
425   // same.
426   const unsigned MaxColumns = 75;
427   unsigned Columns = 0;
428 
429   for (StringRef Target : Targets) {
430     unsigned N = Target.size();
431     if (Columns == 0) {
432       Columns += N;
433     } else if (Columns + N + 2 > MaxColumns) {
434       Columns = N + 2;
435       OS << " \\\n  ";
436     } else {
437       Columns += N + 1;
438       OS << ' ';
439     }
440     // Targets already quoted as needed.
441     OS << Target;
442   }
443 
444   OS << ':';
445   Columns += 1;
446 
447   // Now add each dependency in the order it was seen, but avoiding
448   // duplicates.
449   for (StringRef File : Files) {
450     // Start a new line if this would exceed the column limit. Make
451     // sure to leave space for a trailing " \" in case we need to
452     // break the line on the next iteration.
453     unsigned N = File.size();
454     if (Columns + (N + 1) + 2 > MaxColumns) {
455       OS << " \\\n ";
456       Columns = 2;
457     }
458     OS << ' ';
459     PrintFilename(OS, File, OutputFormat);
460     Columns += N + 1;
461   }
462   OS << '\n';
463 
464   // Create phony targets if requested.
465   if (PhonyTarget && !Files.empty()) {
466     // Skip the first entry, this is always the input file itself.
467     for (auto I = Files.begin() + 1, E = Files.end(); I != E; ++I) {
468       OS << '\n';
469       PrintFilename(OS, *I, OutputFormat);
470       OS << ":\n";
471     }
472   }
473 }
474 
475 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
476                                           bool IsSystem, bool IsOverridden,
477                                           bool IsExplicitModule) {
478   assert(!IsSystem || needsSystemInputFileVisitation());
479   if (IsOverridden || IsExplicitModule)
480     return true;
481 
482   Parent.AddFilename(Filename);
483   return true;
484 }
485 
486 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
487                                            serialization::ModuleKind Kind) {
488   if (Parent.includeModuleFiles())
489     Parent.AddFilename(Filename);
490 }
491