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