blob: 0b9c0d47dc366d67eb7cf8736c43dfa2c96bd34a [file] [log] [blame]
Daniel Dunbar750c3582008-10-24 22:12:41 +00001//===--- 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
Eli Friedmanb09f6e12009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerb9c3f962009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Frontend/DependencyOutputOptions.h"
18#include "clang/Frontend/FrontendDiagnostic.h"
19#include "clang/Lex/DirectoryLookup.h"
Peter Collingbournebb527862011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000021#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070023#include "clang/Serialization/ASTReader.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000024#include "llvm/ADT/StringSet.h"
Benjamin Kramer0b214902013-06-13 14:26:04 +000025#include "llvm/Support/FileSystem.h"
Eli Friedmana6e023c2011-07-08 20:17:28 +000026#include "llvm/Support/Path.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000028
29using namespace clang;
30
31namespace {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070032struct DepCollectorPPCallbacks : public PPCallbacks {
33 DependencyCollector &DepCollector;
34 SourceManager &SM;
35 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
36 : DepCollector(L), SM(SM) { }
37
38 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
39 SrcMgr::CharacteristicKind FileType,
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 =
48 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
49 if (!FE)
50 return;
51
52 StringRef Filename = FE->getName();
53
54 // Remove leading "./" (or ".//" or "././" etc.)
55 while (Filename.size() > 2 && Filename[0] == '.' &&
56 llvm::sys::path::is_separator(Filename[1])) {
57 Filename = Filename.substr(1);
58 while (llvm::sys::path::is_separator(Filename[0]))
59 Filename = Filename.substr(1);
60 }
61
62 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
63 FileType != SrcMgr::C_User,
64 /*IsModuleFile*/false, /*IsMissing*/false);
65 }
66
67 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
68 StringRef FileName, bool IsAngled,
69 CharSourceRange FilenameRange, const FileEntry *File,
70 StringRef SearchPath, StringRef RelativePath,
71 const Module *Imported) override {
72 if (!File)
73 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
74 /*IsSystem*/false, /*IsModuleFile*/false,
75 /*IsMissing*/true);
76 // Files that actually exist are handled by FileChanged.
77 }
78
79 void EndOfMainFile() override {
80 DepCollector.finishedMainFile();
81 }
82};
83
84struct DepCollectorASTListener : public ASTReaderListener {
85 DependencyCollector &DepCollector;
86 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
87 bool needsInputFileVisitation() override { return true; }
88 bool needsSystemInputFileVisitation() override {
89 return DepCollector.needSystemDependencies();
90 }
91 void visitModuleFile(StringRef Filename) override {
92 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
93 /*IsSystem*/false, /*IsModuleFile*/true,
94 /*IsMissing*/false);
95 }
96 bool visitInputFile(StringRef Filename, bool IsSystem,
97 bool IsOverridden) override {
98 if (IsOverridden)
99 return true;
100
101 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
102 /*IsModuleFile*/false, /*IsMissing*/false);
103 return true;
104 }
105};
106} // end anonymous namespace
107
108void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
109 bool IsSystem, bool IsModuleFile,
110 bool IsMissing) {
111 if (Seen.insert(Filename) &&
112 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
113 Dependencies.push_back(Filename);
114}
115
116bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
117 bool IsSystem, bool IsModuleFile,
118 bool IsMissing) {
119 return Filename != "<built-in>" && (needSystemDependencies() || !IsSystem);
120}
121
122DependencyCollector::~DependencyCollector() { }
123void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
124 PP.addPPCallbacks(new DepCollectorPPCallbacks(*this, PP.getSourceManager()));
125}
126void DependencyCollector::attachToASTReader(ASTReader &R) {
127 R.addListener(new DepCollectorASTListener(*this));
128}
129
130namespace {
Stephen Hines651f13c2014-04-23 16:59:28 -0700131/// Private implementation for DependencyFileGenerator
132class DFGImpl : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000133 std::vector<std::string> Files;
134 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000135 const Preprocessor *PP;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000136 std::string OutputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +0000137 std::vector<std::string> Targets;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000138 bool IncludeSystemHeaders;
139 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +0000140 bool AddMissingHeaderDeps;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000141 bool SeenMissingHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -0700142 bool IncludeModuleFiles;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000143private:
144 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +0000145 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000146 void OutputDependencyFile();
147
148public:
Stephen Hines651f13c2014-04-23 16:59:28 -0700149 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000150 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000151 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +0000152 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000153 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Stephen Hines651f13c2014-04-23 16:59:28 -0700154 SeenMissingHeader(false),
155 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000156
Stephen Hines651f13c2014-04-23 16:59:28 -0700157 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
158 SrcMgr::CharacteristicKind FileType,
159 FileID PrevFID) override;
160 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
161 StringRef FileName, bool IsAngled,
162 CharSourceRange FilenameRange, const FileEntry *File,
163 StringRef SearchPath, StringRef RelativePath,
164 const Module *Imported) override;
Daniel Dunbardbd82092010-03-23 05:09:10 +0000165
Stephen Hines651f13c2014-04-23 16:59:28 -0700166 void EndOfMainFile() override {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000167 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000168 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700169
170 void AddFilename(StringRef Filename);
171 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
172 bool includeModuleFiles() const { return IncludeModuleFiles; }
173};
174
175class DFGASTReaderListener : public ASTReaderListener {
176 DFGImpl &Parent;
177public:
178 DFGASTReaderListener(DFGImpl &Parent)
179 : Parent(Parent) { }
180 bool needsInputFileVisitation() override { return true; }
181 bool needsSystemInputFileVisitation() override {
182 return Parent.includeSystemHeaders();
183 }
184 void visitModuleFile(StringRef Filename) override;
185 bool visitInputFile(StringRef Filename, bool isSystem,
186 bool isOverridden) override;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000187};
188}
189
Stephen Hines651f13c2014-04-23 16:59:28 -0700190DependencyFileGenerator::DependencyFileGenerator(void *Impl)
191: Impl(Impl) { }
192
193DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
194 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
195
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000196 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +0000197 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700198 return nullptr;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000199 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000200
Peter Collingbournebb527862011-07-12 19:35:15 +0000201 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +0000202 if (Opts.AddMissingHeaderDeps)
203 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +0000204
Stephen Hines651f13c2014-04-23 16:59:28 -0700205 DFGImpl *Callback = new DFGImpl(&PP, Opts);
206 PP.addPPCallbacks(Callback); // PP owns the Callback
207 return new DependencyFileGenerator(Callback);
208}
209
210void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
211 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
212 assert(I && "missing implementation");
213 R.addListener(new DFGASTReaderListener(*I));
Daniel Dunbar750c3582008-10-24 22:12:41 +0000214}
215
216/// FileMatchesDepCriteria - Determine whether the given Filename should be
217/// considered as a dependency.
Stephen Hines651f13c2014-04-23 16:59:28 -0700218bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
219 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000220 if (strcmp("<built-in>", Filename) == 0)
221 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000222
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000223 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000224 return true;
225
226 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000227}
228
Stephen Hines651f13c2014-04-23 16:59:28 -0700229void DFGImpl::FileChanged(SourceLocation Loc,
230 FileChangeReason Reason,
231 SrcMgr::CharacteristicKind FileType,
232 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000233 if (Reason != PPCallbacks::EnterFile)
234 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000236 // Dependency generation really does want to go all the way to the
237 // file entry for a source location to find out what is depended on.
238 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000239 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000241 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000242 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700243 if (!FE) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner5f9e2722011-07-23 10:55:15 +0000245 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000246 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000247 return;
248
Eli Friedmana6e023c2011-07-08 20:17:28 +0000249 // Remove leading "./" (or ".//" or "././" etc.)
250 while (Filename.size() > 2 && Filename[0] == '.' &&
251 llvm::sys::path::is_separator(Filename[1])) {
252 Filename = Filename.substr(1);
253 while (llvm::sys::path::is_separator(Filename[0]))
254 Filename = Filename.substr(1);
255 }
256
Peter Collingbournebb527862011-07-12 19:35:15 +0000257 AddFilename(Filename);
258}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000259
Stephen Hines651f13c2014-04-23 16:59:28 -0700260void DFGImpl::InclusionDirective(SourceLocation HashLoc,
261 const Token &IncludeTok,
262 StringRef FileName,
263 bool IsAngled,
264 CharSourceRange FilenameRange,
265 const FileEntry *File,
266 StringRef SearchPath,
267 StringRef RelativePath,
268 const Module *Imported) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000269 if (!File) {
270 if (AddMissingHeaderDeps)
271 AddFilename(FileName);
272 else
273 SeenMissingHeader = true;
274 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000275}
276
Stephen Hines651f13c2014-04-23 16:59:28 -0700277void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000278 if (FilesSet.insert(Filename))
279 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000280}
281
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000282/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
283/// other scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000285 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000286 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner9d506342011-02-17 02:14:49 +0000287 OS << '\\';
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000288 else if (Filename[i] == '$') // $ is escaped by $$.
289 OS << '$';
Chris Lattner9d506342011-02-17 02:14:49 +0000290 OS << Filename[i];
291 }
292}
293
Stephen Hines651f13c2014-04-23 16:59:28 -0700294void DFGImpl::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000295 if (SeenMissingHeader) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700296 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000297 return;
298 }
299
300 std::string Err;
Stephen Hines651f13c2014-04-23 16:59:28 -0700301 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err, llvm::sys::fs::F_Text);
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000302 if (!Err.empty()) {
303 PP->getDiagnostics().Report(diag::err_fe_error_opening)
304 << OutputFile << Err;
305 return;
306 }
307
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000308 // Write out the dependency targets, trying to avoid overly long
309 // lines when possible. We try our best to emit exactly the same
310 // dependency file as GCC (4.2), assuming the included files are the
311 // same.
312 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000313 unsigned Columns = 0;
314
315 for (std::vector<std::string>::iterator
316 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
317 unsigned N = I->length();
318 if (Columns == 0) {
319 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000320 } else if (Columns + N + 2 > MaxColumns) {
321 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000322 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000323 } else {
324 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000325 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000326 }
Chris Lattner9d506342011-02-17 02:14:49 +0000327 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000328 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000329 }
330
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000331 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000332 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000334 // Now add each dependency in the order it was seen, but avoiding
335 // duplicates.
336 for (std::vector<std::string>::iterator I = Files.begin(),
337 E = Files.end(); I != E; ++I) {
338 // Start a new line if this would exceed the column limit. Make
339 // sure to leave space for a trailing " \" in case we need to
340 // break the line on the next iteration.
341 unsigned N = I->length();
342 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000343 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000344 Columns = 2;
345 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000346 OS << ' ';
347 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000348 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000349 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000350 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000351
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000352 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000353 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000354 // Skip the first entry, this is always the input file itself.
355 for (std::vector<std::string>::iterator I = Files.begin() + 1,
356 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000357 OS << '\n';
358 PrintFilename(OS, *I);
359 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000360 }
361 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000362}
363
Stephen Hines651f13c2014-04-23 16:59:28 -0700364bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
365 bool IsSystem, bool IsOverridden) {
366 assert(!IsSystem || needsSystemInputFileVisitation());
367 if (IsOverridden)
368 return true;
369
370 Parent.AddFilename(Filename);
371 return true;
372}
373
374void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
375 if (Parent.includeModuleFiles())
376 Parent.AddFilename(Filename);
377}