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