clang  7.0.0svn
DependencyFile.cpp
Go to the documentation of this file.
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"
21 #include "clang/Lex/ModuleMap.h"
22 #include "clang/Lex/PPCallbacks.h"
23 #include "clang/Lex/Preprocessor.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;
37  DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
38  : DepCollector(L), SM(SM) { }
39 
40  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
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 =
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 
143  PP.addPPCallbacks(
144  llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
146  llvm::make_unique<DepCollectorMMCallbacks>(*this));
147 }
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  unsigned InputFileIndex;
167 
168 private:
169  bool FileMatchesDepCriteria(const char *Filename,
170  SrcMgr::CharacteristicKind FileType);
171  void OutputDependencyFile();
172 
173 public:
174  DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
175  : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
176  IncludeSystemHeaders(Opts.IncludeSystemHeaders),
177  PhonyTarget(Opts.UsePhonyTargets),
178  AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
179  SeenMissingHeader(false),
180  IncludeModuleFiles(Opts.IncludeModuleFiles),
181  OutputFormat(Opts.OutputFormat),
182  InputFileIndex(0) {
183  for (const auto &ExtraDep : Opts.ExtraDeps) {
184  if (AddFilename(ExtraDep))
185  ++InputFileIndex;
186  }
187  }
188 
189  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
191  FileID PrevFID) override;
192 
193  void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
194  SrcMgr::CharacteristicKind FileType) override;
195 
196  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
197  StringRef FileName, bool IsAngled,
198  CharSourceRange FilenameRange, const FileEntry *File,
199  StringRef SearchPath, StringRef RelativePath,
200  const Module *Imported,
201  SrcMgr::CharacteristicKind FileType) override;
202 
203  void EndOfMainFile() override {
204  OutputDependencyFile();
205  }
206 
207  bool AddFilename(StringRef Filename);
208  bool includeSystemHeaders() const { return IncludeSystemHeaders; }
209  bool includeModuleFiles() const { return IncludeModuleFiles; }
210 };
211 
212 class DFGMMCallback : public ModuleMapCallbacks {
213  DFGImpl &Parent;
214 public:
215  DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
216  void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
217  bool IsSystem) override {
218  if (!IsSystem || Parent.includeSystemHeaders())
219  Parent.AddFilename(Entry.getName());
220  }
221 };
222 
223 class DFGASTReaderListener : public ASTReaderListener {
224  DFGImpl &Parent;
225 public:
226  DFGASTReaderListener(DFGImpl &Parent)
227  : Parent(Parent) { }
228  bool needsInputFileVisitation() override { return true; }
229  bool needsSystemInputFileVisitation() override {
230  return Parent.includeSystemHeaders();
231  }
232  void visitModuleFile(StringRef Filename,
233  serialization::ModuleKind Kind) override;
234  bool visitInputFile(StringRef Filename, bool isSystem,
235  bool isOverridden, bool isExplicitModule) override;
236 };
237 }
238 
239 DependencyFileGenerator::DependencyFileGenerator(void *Impl)
240 : Impl(Impl) { }
241 
244 
245  if (Opts.Targets.empty()) {
246  PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
247  return nullptr;
248  }
249 
250  // Disable the "file not found" diagnostic if the -MG option was given.
251  if (Opts.AddMissingHeaderDeps)
253 
254  DFGImpl *Callback = new DFGImpl(&PP, Opts);
255  PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
257  llvm::make_unique<DFGMMCallback>(*Callback));
258  return new DependencyFileGenerator(Callback);
259 }
260 
262  DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
263  assert(I && "missing implementation");
264  R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
265 }
266 
267 /// FileMatchesDepCriteria - Determine whether the given Filename should be
268 /// considered as a dependency.
269 bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
270  SrcMgr::CharacteristicKind FileType) {
271  if (isSpecialFilename(Filename))
272  return false;
273 
274  if (IncludeSystemHeaders)
275  return true;
276 
277  return !isSystem(FileType);
278 }
279 
280 void DFGImpl::FileChanged(SourceLocation Loc,
281  FileChangeReason Reason,
283  FileID PrevFID) {
284  if (Reason != PPCallbacks::EnterFile)
285  return;
286 
287  // Dependency generation really does want to go all the way to the
288  // file entry for a source location to find out what is depended on.
289  // We do not want #line markers to affect dependency generation!
290  SourceManager &SM = PP->getSourceManager();
291 
292  const FileEntry *FE =
294  if (!FE) return;
295 
296  StringRef Filename = FE->getName();
297  if (!FileMatchesDepCriteria(Filename.data(), FileType))
298  return;
299 
300  AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
301 }
302 
303 void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
304  const Token &FilenameTok,
305  SrcMgr::CharacteristicKind FileType) {
306  StringRef Filename = SkippedFile.getName();
307  if (!FileMatchesDepCriteria(Filename.data(), FileType))
308  return;
309 
310  AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
311 }
312 
313 void DFGImpl::InclusionDirective(SourceLocation HashLoc,
314  const Token &IncludeTok,
315  StringRef FileName,
316  bool IsAngled,
317  CharSourceRange FilenameRange,
318  const FileEntry *File,
319  StringRef SearchPath,
320  StringRef RelativePath,
321  const Module *Imported,
322  SrcMgr::CharacteristicKind FileType) {
323  if (!File) {
324  if (AddMissingHeaderDeps)
325  AddFilename(FileName);
326  else
327  SeenMissingHeader = true;
328  }
329 }
330 
331 bool DFGImpl::AddFilename(StringRef Filename) {
332  if (FilesSet.insert(Filename).second) {
333  Files.push_back(Filename);
334  return true;
335  }
336  return false;
337 }
338 
339 /// Print the filename, with escaping or quoting that accommodates the three
340 /// most likely tools that use dependency files: GNU Make, BSD Make, and
341 /// NMake/Jom.
342 ///
343 /// BSD Make is the simplest case: It does no escaping at all. This means
344 /// characters that are normally delimiters, i.e. space and # (the comment
345 /// character) simply aren't supported in filenames.
346 ///
347 /// GNU Make does allow space and # in filenames, but to avoid being treated
348 /// as a delimiter or comment, these must be escaped with a backslash. Because
349 /// backslash is itself the escape character, if a backslash appears in a
350 /// filename, it should be escaped as well. (As a special case, $ is escaped
351 /// as $$, which is the normal Make way to handle the $ character.)
352 /// For compatibility with BSD Make and historical practice, if GNU Make
353 /// un-escapes characters in a filename but doesn't find a match, it will
354 /// retry with the unmodified original string.
355 ///
356 /// GCC tries to accommodate both Make formats by escaping any space or #
357 /// characters in the original filename, but not escaping backslashes. The
358 /// apparent intent is so that filenames with backslashes will be handled
359 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
360 /// unmodified original string; filenames with # or space characters aren't
361 /// supported by BSD Make at all, but will be handled correctly by GNU Make
362 /// due to the escaping.
363 ///
364 /// A corner case that GCC gets only partly right is when the original filename
365 /// has a backslash immediately followed by space or #. GNU Make would expect
366 /// this backslash to be escaped; however GCC escapes the original backslash
367 /// only when followed by space, not #. It will therefore take a dependency
368 /// from a directive such as
369 /// #include "a\ b\#c.h"
370 /// and emit it as
371 /// a\\\ b\\#c.h
372 /// which GNU Make will interpret as
373 /// a\ b\
374 /// followed by a comment. Failing to find this file, it will fall back to the
375 /// original string, which probably doesn't exist either; in any case it won't
376 /// find
377 /// a\ b\#c.h
378 /// which is the actual filename specified by the include directive.
379 ///
380 /// Clang does what GCC does, rather than what GNU Make expects.
381 ///
382 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
383 /// double-quotes to avoid misinterpreting them; see
384 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
385 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
386 /// for Windows file-naming info.
387 static void PrintFilename(raw_ostream &OS, StringRef Filename,
388  DependencyOutputFormat OutputFormat) {
389  if (OutputFormat == DependencyOutputFormat::NMake) {
390  // Add quotes if needed. These are the characters listed as "special" to
391  // NMake, that are legal in a Windows filespec, and that could cause
392  // misinterpretation of the dependency string.
393  if (Filename.find_first_of(" #${}^!") != StringRef::npos)
394  OS << '\"' << Filename << '\"';
395  else
396  OS << Filename;
397  return;
398  }
399  assert(OutputFormat == DependencyOutputFormat::Make);
400  for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
401  if (Filename[i] == '#') // Handle '#' the broken gcc way.
402  OS << '\\';
403  else if (Filename[i] == ' ') { // Handle space correctly.
404  OS << '\\';
405  unsigned j = i;
406  while (j > 0 && Filename[--j] == '\\')
407  OS << '\\';
408  } else if (Filename[i] == '$') // $ is escaped by $$.
409  OS << '$';
410  OS << Filename[i];
411  }
412 }
413 
414 void DFGImpl::OutputDependencyFile() {
415  if (SeenMissingHeader) {
416  llvm::sys::fs::remove(OutputFile);
417  return;
418  }
419 
420  std::error_code EC;
421  llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
422  if (EC) {
423  PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
424  << EC.message();
425  return;
426  }
427 
428  // Write out the dependency targets, trying to avoid overly long
429  // lines when possible. We try our best to emit exactly the same
430  // dependency file as GCC (4.2), assuming the included files are the
431  // same.
432  const unsigned MaxColumns = 75;
433  unsigned Columns = 0;
434 
435  for (StringRef Target : Targets) {
436  unsigned N = Target.size();
437  if (Columns == 0) {
438  Columns += N;
439  } else if (Columns + N + 2 > MaxColumns) {
440  Columns = N + 2;
441  OS << " \\\n ";
442  } else {
443  Columns += N + 1;
444  OS << ' ';
445  }
446  // Targets already quoted as needed.
447  OS << Target;
448  }
449 
450  OS << ':';
451  Columns += 1;
452 
453  // Now add each dependency in the order it was seen, but avoiding
454  // duplicates.
455  for (StringRef File : Files) {
456  // Start a new line if this would exceed the column limit. Make
457  // sure to leave space for a trailing " \" in case we need to
458  // break the line on the next iteration.
459  unsigned N = File.size();
460  if (Columns + (N + 1) + 2 > MaxColumns) {
461  OS << " \\\n ";
462  Columns = 2;
463  }
464  OS << ' ';
465  PrintFilename(OS, File, OutputFormat);
466  Columns += N + 1;
467  }
468  OS << '\n';
469 
470  // Create phony targets if requested.
471  if (PhonyTarget && !Files.empty()) {
472  unsigned Index = 0;
473  for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
474  if (Index++ == InputFileIndex)
475  continue;
476  OS << '\n';
477  PrintFilename(OS, *I, OutputFormat);
478  OS << ":\n";
479  }
480  }
481 }
482 
483 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
484  bool IsSystem, bool IsOverridden,
485  bool IsExplicitModule) {
486  assert(!IsSystem || needsSystemInputFileVisitation());
487  if (IsOverridden || IsExplicitModule)
488  return true;
489 
490  Parent.AddFilename(Filename);
491  return true;
492 }
493 
494 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
496  if (Parent.includeModuleFiles())
497  Parent.AddFilename(Filename);
498 }
Defines the clang::FileManager interface and associated types.
An interface for collecting the dependencies of a compilation.
Definition: Utils.h:83
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:1294
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:649
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:36
unsigned IncludeSystemHeaders
Include system header dependencies.
virtual bool needSystemDependencies()
Return true if system files should be passed to sawDependency().
Definition: Utils.h:102
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
Describes a module or submodule.
Definition: Module.h:65
void SetSuppressIncludeNotFoundError(bool Suppress)
Definition: Preprocessor.h:864
virtual void attachToASTReader(ASTReader &R)
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:821
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:412
Builds a depdenency file when attached to a Preprocessor (for includes) and ASTReader (for module imp...
Definition: Utils.h:119
NodeId Parent
Definition: ASTDiff.cpp:192
static DependencyFileGenerator * CreateAndAttachToPreprocessor(Preprocessor &PP, const DependencyOutputOptions &Opts)
ModuleKind
Specifies the kind of module that has been loaded.
Definition: Module.h:43
StringRef Filename
Definition: Format.cpp:1598
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:99
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:47
Record the location of an inclusion directive, such as an #include or #import statement.
const SourceManager & SM
Definition: Format.cpp:1468
SourceManager & getSourceManager() const
Definition: Preprocessor.h:819
Kind
void addListener(std::unique_ptr< ASTReaderListener > L)
Add an AST callback listener.
Definition: ASTReader.h:1566
void AttachToASTReader(ASTReader &R)
Encodes a location in the source.
StringRef getName() const
Definition: FileManager.h:84
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:812
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:83
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:912
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:127