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/LexDiagnostic.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)));
53 StringRef Filename = FE->getName();
55 // Remove leading "./" (or ".//" or "././" etc.)
56 while (Filename.size() > 2 && Filename[0] == '.' &&
57 llvm::sys::path::is_separator(Filename[1])) {
58 Filename = Filename.substr(1);
59 while (llvm::sys::path::is_separator(Filename[0]))
60 Filename = Filename.substr(1);
63 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
64 FileType != SrcMgr::C_User,
65 /*IsModuleFile*/false, /*IsMissing*/false);
68 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
69 StringRef FileName, bool IsAngled,
70 CharSourceRange FilenameRange, const FileEntry *File,
71 StringRef SearchPath, StringRef RelativePath,
72 const Module *Imported) override {
74 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
75 /*IsSystem*/false, /*IsModuleFile*/false,
77 // Files that actually exist are handled by FileChanged.
80 void EndOfMainFile() override {
81 DepCollector.finishedMainFile();
85 struct DepCollectorASTListener : public ASTReaderListener {
86 DependencyCollector &DepCollector;
87 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
88 bool needsInputFileVisitation() override { return true; }
89 bool needsSystemInputFileVisitation() override {
90 return DepCollector.needSystemDependencies();
92 void visitModuleFile(StringRef Filename) override {
93 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
94 /*IsSystem*/false, /*IsModuleFile*/true,
97 bool visitInputFile(StringRef Filename, bool IsSystem,
98 bool IsOverridden) override {
102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
103 /*IsModuleFile*/false, /*IsMissing*/false);
107 } // end anonymous namespace
109 void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
110 bool IsSystem, bool IsModuleFile,
112 if (Seen.insert(Filename).second &&
113 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
114 Dependencies.push_back(Filename);
117 static bool isSpecialFilename(StringRef Filename) {
118 return llvm::StringSwitch<bool>(Filename)
119 .Case("<built-in>", true)
120 .Case("<stdin>", true)
124 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
125 bool IsSystem, bool IsModuleFile,
127 return !isSpecialFilename(Filename) &&
128 (needSystemDependencies() || !IsSystem);
131 DependencyCollector::~DependencyCollector() { }
132 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
134 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
136 void DependencyCollector::attachToASTReader(ASTReader &R) {
137 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
141 /// Private implementation for DependencyFileGenerator
142 class DFGImpl : public PPCallbacks {
143 std::vector<std::string> Files;
144 llvm::StringSet<> FilesSet;
145 const Preprocessor *PP;
146 std::string OutputFile;
147 std::vector<std::string> Targets;
148 bool IncludeSystemHeaders;
150 bool AddMissingHeaderDeps;
151 bool SeenMissingHeader;
152 bool IncludeModuleFiles;
153 DependencyOutputFormat OutputFormat;
156 bool FileMatchesDepCriteria(const char *Filename,
157 SrcMgr::CharacteristicKind FileType);
158 void OutputDependencyFile();
161 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
162 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
163 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
164 PhonyTarget(Opts.UsePhonyTargets),
165 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
166 SeenMissingHeader(false),
167 IncludeModuleFiles(Opts.IncludeModuleFiles),
168 OutputFormat(Opts.OutputFormat) {}
170 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
171 SrcMgr::CharacteristicKind FileType,
172 FileID PrevFID) override;
173 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
174 StringRef FileName, bool IsAngled,
175 CharSourceRange FilenameRange, const FileEntry *File,
176 StringRef SearchPath, StringRef RelativePath,
177 const Module *Imported) override;
179 void EndOfMainFile() override {
180 OutputDependencyFile();
183 void AddFilename(StringRef Filename);
184 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
185 bool includeModuleFiles() const { return IncludeModuleFiles; }
188 class DFGASTReaderListener : public ASTReaderListener {
191 DFGASTReaderListener(DFGImpl &Parent)
193 bool needsInputFileVisitation() override { return true; }
194 bool needsSystemInputFileVisitation() override {
195 return Parent.includeSystemHeaders();
197 void visitModuleFile(StringRef Filename) override;
198 bool visitInputFile(StringRef Filename, bool isSystem,
199 bool isOverridden) override;
203 DependencyFileGenerator::DependencyFileGenerator(void *Impl)
206 DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
207 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
209 if (Opts.Targets.empty()) {
210 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
214 // Disable the "file not found" diagnostic if the -MG option was given.
215 if (Opts.AddMissingHeaderDeps)
216 PP.SetSuppressIncludeNotFoundError(true);
218 DFGImpl *Callback = new DFGImpl(&PP, Opts);
219 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
220 return new DependencyFileGenerator(Callback);
223 void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
224 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
225 assert(I && "missing implementation");
226 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
229 /// FileMatchesDepCriteria - Determine whether the given Filename should be
230 /// considered as a dependency.
231 bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
232 SrcMgr::CharacteristicKind FileType) {
233 if (isSpecialFilename(Filename))
236 if (IncludeSystemHeaders)
239 return FileType == SrcMgr::C_User;
242 void DFGImpl::FileChanged(SourceLocation Loc,
243 FileChangeReason Reason,
244 SrcMgr::CharacteristicKind FileType,
246 if (Reason != PPCallbacks::EnterFile)
249 // Dependency generation really does want to go all the way to the
250 // file entry for a source location to find out what is depended on.
251 // We do not want #line markers to affect dependency generation!
252 SourceManager &SM = PP->getSourceManager();
254 const FileEntry *FE =
255 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
258 StringRef Filename = FE->getName();
259 if (!FileMatchesDepCriteria(Filename.data(), FileType))
262 // Remove leading "./" (or ".//" or "././" etc.)
263 while (Filename.size() > 2 && Filename[0] == '.' &&
264 llvm::sys::path::is_separator(Filename[1])) {
265 Filename = Filename.substr(1);
266 while (llvm::sys::path::is_separator(Filename[0]))
267 Filename = Filename.substr(1);
270 AddFilename(Filename);
273 void DFGImpl::InclusionDirective(SourceLocation HashLoc,
274 const Token &IncludeTok,
277 CharSourceRange FilenameRange,
278 const FileEntry *File,
279 StringRef SearchPath,
280 StringRef RelativePath,
281 const Module *Imported) {
283 if (AddMissingHeaderDeps)
284 AddFilename(FileName);
286 SeenMissingHeader = true;
290 void DFGImpl::AddFilename(StringRef Filename) {
291 if (FilesSet.insert(Filename).second)
292 Files.push_back(Filename);
295 /// Print the filename, with escaping or quoting that accommodates the three
296 /// most likely tools that use dependency files: GNU Make, BSD Make, and
299 /// BSD Make is the simplest case: It does no escaping at all. This means
300 /// characters that are normally delimiters, i.e. space and # (the comment
301 /// character) simply aren't supported in filenames.
303 /// GNU Make does allow space and # in filenames, but to avoid being treated
304 /// as a delimiter or comment, these must be escaped with a backslash. Because
305 /// backslash is itself the escape character, if a backslash appears in a
306 /// filename, it should be escaped as well. (As a special case, $ is escaped
307 /// as $$, which is the normal Make way to handle the $ character.)
308 /// For compatibility with BSD Make and historical practice, if GNU Make
309 /// un-escapes characters in a filename but doesn't find a match, it will
310 /// retry with the unmodified original string.
312 /// GCC tries to accommodate both Make formats by escaping any space or #
313 /// characters in the original filename, but not escaping backslashes. The
314 /// apparent intent is so that filenames with backslashes will be handled
315 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
316 /// unmodified original string; filenames with # or space characters aren't
317 /// supported by BSD Make at all, but will be handled correctly by GNU Make
318 /// due to the escaping.
320 /// A corner case that GCC gets only partly right is when the original filename
321 /// has a backslash immediately followed by space or #. GNU Make would expect
322 /// this backslash to be escaped; however GCC escapes the original backslash
323 /// only when followed by space, not #. It will therefore take a dependency
324 /// from a directive such as
325 /// #include "a\ b\#c.h"
328 /// which GNU Make will interpret as
330 /// followed by a comment. Failing to find this file, it will fall back to the
331 /// original string, which probably doesn't exist either; in any case it won't
334 /// which is the actual filename specified by the include directive.
336 /// Clang does what GCC does, rather than what GNU Make expects.
338 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
339 /// double-quotes to avoid misinterpreting them; see
340 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
341 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
342 /// for Windows file-naming info.
343 static void PrintFilename(raw_ostream &OS, StringRef Filename,
344 DependencyOutputFormat OutputFormat) {
345 if (OutputFormat == DependencyOutputFormat::NMake) {
346 // Add quotes if needed. These are the characters listed as "special" to
347 // NMake, that are legal in a Windows filespec, and that could cause
348 // misinterpretation of the dependency string.
349 if (Filename.find_first_of(" #${}^!") != StringRef::npos)
350 OS << '\"' << Filename << '\"';
355 assert(OutputFormat == DependencyOutputFormat::Make);
356 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
357 if (Filename[i] == '#') // Handle '#' the broken gcc way.
359 else if (Filename[i] == ' ') { // Handle space correctly.
362 while (j > 0 && Filename[--j] == '\\')
364 } else if (Filename[i] == '$') // $ is escaped by $$.
370 void DFGImpl::OutputDependencyFile() {
371 if (SeenMissingHeader) {
372 llvm::sys::fs::remove(OutputFile);
377 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
379 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
384 // Write out the dependency targets, trying to avoid overly long
385 // lines when possible. We try our best to emit exactly the same
386 // dependency file as GCC (4.2), assuming the included files are the
388 const unsigned MaxColumns = 75;
389 unsigned Columns = 0;
391 for (std::vector<std::string>::iterator
392 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
393 unsigned N = I->length();
396 } else if (Columns + N + 2 > MaxColumns) {
403 // Targets already quoted as needed.
410 // Now add each dependency in the order it was seen, but avoiding
412 for (std::vector<std::string>::iterator I = Files.begin(),
413 E = Files.end(); I != E; ++I) {
414 // Start a new line if this would exceed the column limit. Make
415 // sure to leave space for a trailing " \" in case we need to
416 // break the line on the next iteration.
417 unsigned N = I->length();
418 if (Columns + (N + 1) + 2 > MaxColumns) {
423 PrintFilename(OS, *I, OutputFormat);
428 // Create phony targets if requested.
429 if (PhonyTarget && !Files.empty()) {
430 // Skip the first entry, this is always the input file itself.
431 for (std::vector<std::string>::iterator I = Files.begin() + 1,
432 E = Files.end(); I != E; ++I) {
434 PrintFilename(OS, *I, OutputFormat);
440 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
441 bool IsSystem, bool IsOverridden) {
442 assert(!IsSystem || needsSystemInputFileVisitation());
446 Parent.AddFilename(Filename);
450 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
451 if (Parent.includeModuleFiles())
452 Parent.AddFilename(Filename);