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