blob: ce3308659c05fc8be96315118a773b70ac2fc3e7 [file] [log] [blame]
Daniel Dunbarfa0caca2008-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 Friedman16b7b6f2009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar89d1fdf2009-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 Collingbourne77b0e7f22011-07-12 19:35:15 +000020#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000021#include "clang/Lex/PPCallbacks.h"
22#include "clang/Lex/Preprocessor.h"
Ben Langmuircb69b572014-03-07 06:40:32 +000023#include "clang/Serialization/ASTReader.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000024#include "llvm/ADT/StringSet.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000025#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000026#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000028
29using namespace clang;
30
31namespace {
Ben Langmuir33c80902014-06-30 20:04:14 +000032struct 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) {
David Blaikie2721c322014-08-10 16:54:39 +0000127 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000128}
129
130namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000131/// Private implementation for DependencyFileGenerator
132class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000133 std::vector<std::string> Files;
134 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000135 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000136 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000137 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000138 bool IncludeSystemHeaders;
139 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000140 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000141 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000142 bool IncludeModuleFiles;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000143private:
144 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000145 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000146 void OutputDependencyFile();
147
148public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000149 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000150 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000151 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000152 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000153 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000154 SeenMissingHeader(false),
155 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000156
Craig Topperafa7cb32014-03-13 06:07:04 +0000157 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 Dunbarcb9eaf52010-03-23 05:09:10 +0000165
Craig Topperafa7cb32014-03-13 06:07:04 +0000166 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000167 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000168 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000169
170 void AddFilename(StringRef Filename);
171 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000172 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000173};
174
175class DFGASTReaderListener : public ASTReaderListener {
176 DFGImpl &Parent;
177public:
178 DFGASTReaderListener(DFGImpl &Parent)
179 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000180 bool needsInputFileVisitation() override { return true; }
181 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000182 return Parent.includeSystemHeaders();
183 }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000184 void visitModuleFile(StringRef Filename) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000185 bool visitInputFile(StringRef Filename, bool isSystem,
186 bool isOverridden) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000187};
188}
189
Ben Langmuircb69b572014-03-07 06:40:32 +0000190DependencyFileGenerator::DependencyFileGenerator(void *Impl)
191: Impl(Impl) { }
192
193DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
194 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
195
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000196 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000197 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000198 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000199 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000200
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000201 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000202 if (Opts.AddMissingHeaderDeps)
203 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000204
Ben Langmuircb69b572014-03-07 06:40:32 +0000205 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");
David Blaikie2721c322014-08-10 16:54:39 +0000213 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000214}
215
216/// FileMatchesDepCriteria - Determine whether the given Filename should be
217/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000218bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
219 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000220 if (strcmp("<built-in>", Filename) == 0)
221 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000222
Eli Friedman33cd03c2009-05-19 03:35:57 +0000223 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000224 return true;
225
226 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000227}
228
Ben Langmuircb69b572014-03-07 06:40:32 +0000229void DFGImpl::FileChanged(SourceLocation Loc,
230 FileChangeReason Reason,
231 SrcMgr::CharacteristicKind FileType,
232 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000233 if (Reason != PPCallbacks::EnterFile)
234 return;
Mike Stump11289f42009-09-09 15:08:12 +0000235
Daniel Dunbar52e96cc2009-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 Lattnerf1ca7d32009-01-27 07:57:44 +0000239 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner64d8fc22009-01-28 05:42:38 +0000241 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000242 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000243 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000245 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000246 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000247 return;
248
Eli Friedmanf7ca26a2011-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 Collingbourne77b0e7f22011-07-12 19:35:15 +0000257 AddFilename(Filename);
258}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000259
Ben Langmuircb69b572014-03-07 06:40:32 +0000260void 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 Collingbourne0e7e3fc2011-11-21 00:01:14 +0000269 if (!File) {
270 if (AddMissingHeaderDeps)
271 AddFilename(FileName);
272 else
273 SeenMissingHeader = true;
274 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000275}
276
Ben Langmuircb69b572014-03-07 06:40:32 +0000277void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000278 if (FilesSet.insert(Filename))
279 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000280}
281
Benjamin Kramerae611512013-04-02 13:38:48 +0000282/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
283/// other scary characters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000285 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerae611512013-04-02 13:38:48 +0000286 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000287 OS << '\\';
Benjamin Kramerae611512013-04-02 13:38:48 +0000288 else if (Filename[i] == '$') // $ is escaped by $$.
289 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000290 OS << Filename[i];
291 }
292}
293
Ben Langmuircb69b572014-03-07 06:40:32 +0000294void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000295 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000296 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000297 return;
298 }
299
Rafael Espindoladae941a2014-08-25 18:17:04 +0000300 std::error_code EC;
301 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
302 if (EC) {
303 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
304 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000305 return;
306 }
307
Daniel Dunbar966c2e12008-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 Lattnerfa5880e2009-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 Lattnerfa5880e2009-01-11 19:28:34 +0000320 } else if (Columns + N + 2 > MaxColumns) {
321 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000322 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000323 } else {
324 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000325 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000326 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000327 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000328 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000329 }
330
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000331 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000332 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Daniel Dunbar966c2e12008-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 Collingbourne0e7e3fc2011-11-21 00:01:14 +0000343 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000344 Columns = 2;
345 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000346 OS << ' ';
347 PrintFilename(OS, *I);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000348 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000349 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000350 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000351
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000352 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000353 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-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 Collingbourne0e7e3fc2011-11-21 00:01:14 +0000357 OS << '\n';
358 PrintFilename(OS, *I);
359 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000360 }
361 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000362}
363
Ben Langmuircb69b572014-03-07 06:40:32 +0000364bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000365 bool IsSystem, bool IsOverridden) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000366 assert(!IsSystem || needsSystemInputFileVisitation());
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000367 if (IsOverridden)
368 return true;
369
Ben Langmuircb69b572014-03-07 06:40:32 +0000370 Parent.AddFilename(Filename);
371 return true;
372}
373
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000374void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
375 if (Parent.includeModuleFiles())
376 Parent.AddFilename(Filename);
377}