1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code generates dependency files.
12 //===----------------------------------------------------------------------===//
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"
30 using namespace clang;
33 struct DepCollectorPPCallbacks : public PPCallbacks {
34 DependencyCollector &DepCollector;
36 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
37 : DepCollector(L), SM(SM) { }
39 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
40 SrcMgr::CharacteristicKind FileType,
41 FileID PrevFID) override {
42 if (Reason != PPCallbacks::EnterFile)
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!
49 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
54 llvm::sys::path::remove_leading_dotslash(FE->getName());
56 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
58 /*IsModuleFile*/false, /*IsMissing*/false);
61 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 {
68 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
69 /*IsSystem*/false, /*IsModuleFile*/false,
71 // Files that actually exist are handled by FileChanged.
74 void EndOfMainFile() override {
75 DepCollector.finishedMainFile();
79 struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
80 DependencyCollector &DepCollector;
81 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
83 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
84 bool IsSystem) override {
85 StringRef Filename = Entry.getName();
86 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
88 /*IsModuleFile*/false,
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();
100 void visitModuleFile(StringRef Filename,
101 serialization::ModuleKind Kind) override {
102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
103 /*IsSystem*/false, /*IsModuleFile*/true,
106 bool visitInputFile(StringRef Filename, bool IsSystem,
107 bool IsOverridden, bool IsExplicitModule) override {
108 if (IsOverridden || IsExplicitModule)
111 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
112 /*IsModuleFile*/false, /*IsMissing*/false);
116 } // end anonymous namespace
118 void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
119 bool IsSystem, bool IsModuleFile,
121 if (Seen.insert(Filename).second &&
122 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
123 Dependencies.push_back(Filename);
126 static bool isSpecialFilename(StringRef Filename) {
127 return llvm::StringSwitch<bool>(Filename)
128 .Case("<built-in>", true)
129 .Case("<stdin>", true)
133 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
134 bool IsSystem, bool IsModuleFile,
136 return !isSpecialFilename(Filename) &&
137 (needSystemDependencies() || !IsSystem);
140 DependencyCollector::~DependencyCollector() { }
141 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
143 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
144 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
145 llvm::make_unique<DepCollectorMMCallbacks>(*this));
147 void DependencyCollector::attachToASTReader(ASTReader &R) {
148 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
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;
161 bool AddMissingHeaderDeps;
162 bool SeenMissingHeader;
163 bool IncludeModuleFiles;
164 DependencyOutputFormat OutputFormat;
165 unsigned InputFileIndex;
168 bool FileMatchesDepCriteria(const char *Filename,
169 SrcMgr::CharacteristicKind FileType);
170 void OutputDependencyFile();
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),
182 for (const auto &ExtraDep : Opts.ExtraDeps) {
183 if (AddFilename(ExtraDep))
188 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
189 SrcMgr::CharacteristicKind FileType,
190 FileID PrevFID) override;
192 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
193 SrcMgr::CharacteristicKind FileType) override;
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;
202 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
203 const FileEntry *File,
204 SrcMgr::CharacteristicKind FileType) override;
206 void EndOfMainFile() override {
207 OutputDependencyFile();
210 bool AddFilename(StringRef Filename);
211 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
212 bool includeModuleFiles() const { return IncludeModuleFiles; }
215 class DFGMMCallback : public ModuleMapCallbacks {
218 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
219 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
220 bool IsSystem) override {
221 if (!IsSystem || Parent.includeSystemHeaders())
222 Parent.AddFilename(Entry.getName());
226 class DFGASTReaderListener : public ASTReaderListener {
229 DFGASTReaderListener(DFGImpl &Parent)
231 bool needsInputFileVisitation() override { return true; }
232 bool needsSystemInputFileVisitation() override {
233 return Parent.includeSystemHeaders();
235 void visitModuleFile(StringRef Filename,
236 serialization::ModuleKind Kind) override;
237 bool visitInputFile(StringRef Filename, bool isSystem,
238 bool isOverridden, bool isExplicitModule) override;
242 DependencyFileGenerator::DependencyFileGenerator(void *Impl)
245 DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
246 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
248 if (Opts.Targets.empty()) {
249 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
253 // Disable the "file not found" diagnostic if the -MG option was given.
254 if (Opts.AddMissingHeaderDeps)
255 PP.SetSuppressIncludeNotFoundError(true);
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);
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));
270 /// FileMatchesDepCriteria - Determine whether the given Filename should be
271 /// considered as a dependency.
272 bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
273 SrcMgr::CharacteristicKind FileType) {
274 if (isSpecialFilename(Filename))
277 if (IncludeSystemHeaders)
280 return !isSystem(FileType);
283 void DFGImpl::FileChanged(SourceLocation Loc,
284 FileChangeReason Reason,
285 SrcMgr::CharacteristicKind FileType,
287 if (Reason != PPCallbacks::EnterFile)
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();
295 const FileEntry *FE =
296 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
299 StringRef Filename = FE->getName();
300 if (!FileMatchesDepCriteria(Filename.data(), FileType))
303 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
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))
313 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
316 void DFGImpl::InclusionDirective(SourceLocation HashLoc,
317 const Token &IncludeTok,
320 CharSourceRange FilenameRange,
321 const FileEntry *File,
322 StringRef SearchPath,
323 StringRef RelativePath,
324 const Module *Imported,
325 SrcMgr::CharacteristicKind FileType) {
327 if (AddMissingHeaderDeps)
328 AddFilename(FileName);
330 SeenMissingHeader = true;
334 void DFGImpl::HasInclude(SourceLocation Loc, StringRef SpelledFilename,
335 bool IsAngled, const FileEntry *File,
336 SrcMgr::CharacteristicKind FileType) {
339 StringRef Filename = File->getName();
340 if (!FileMatchesDepCriteria(Filename.data(), FileType))
342 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
345 bool DFGImpl::AddFilename(StringRef Filename) {
346 if (FilesSet.insert(Filename).second) {
347 Files.push_back(Filename);
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
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.
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.
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.
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"
386 /// which GNU Make will interpret as
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
392 /// which is the actual filename specified by the include directive.
394 /// Clang does what GCC does, rather than what GNU Make expects.
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.
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);
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 << '\"';
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.
421 else if (NativePath[i] == ' ') { // Handle space correctly.
424 while (j > 0 && NativePath[--j] == '\\')
426 } else if (NativePath[i] == '$') // $ is escaped by $$.
432 void DFGImpl::OutputDependencyFile() {
433 if (SeenMissingHeader) {
434 llvm::sys::fs::remove(OutputFile);
439 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
441 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
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
450 const unsigned MaxColumns = 75;
451 unsigned Columns = 0;
453 for (StringRef Target : Targets) {
454 unsigned N = Target.size();
457 } else if (Columns + N + 2 > MaxColumns) {
464 // Targets already quoted as needed.
471 // Now add each dependency in the order it was seen, but avoiding
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) {
483 PrintFilename(OS, File, OutputFormat);
488 // Create phony targets if requested.
489 if (PhonyTarget && !Files.empty()) {
491 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
492 if (Index++ == InputFileIndex)
495 PrintFilename(OS, *I, OutputFormat);
501 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
502 bool IsSystem, bool IsOverridden,
503 bool IsExplicitModule) {
504 assert(!IsSystem || needsSystemInputFileVisitation());
505 if (IsOverridden || IsExplicitModule)
508 Parent.AddFilename(Filename);
512 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
513 serialization::ModuleKind Kind) {
514 if (Parent.includeModuleFiles())
515 Parent.AddFilename(Filename);