blob: 6ea8f5193ef78852fc057721ca858b0c0fb385ee [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"
Stephen Hines176edba2014-12-01 14:53:08 -080025#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer0b214902013-06-13 14:26:04 +000026#include "llvm/Support/FileSystem.h"
Eli Friedmana6e023c2011-07-08 20:17:28 +000027#include "llvm/Support/Path.h"
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar750c3582008-10-24 22:12:41 +000029
30using namespace clang;
31
32namespace {
Stephen Hinesc568f1e2014-07-21 00:47:37 -070033struct DepCollectorPPCallbacks : public PPCallbacks {
34 DependencyCollector &DepCollector;
35 SourceManager &SM;
36 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
37 : DepCollector(L), SM(SM) { }
38
39 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
40 SrcMgr::CharacteristicKind FileType,
41 FileID PrevFID) override {
42 if (Reason != PPCallbacks::EnterFile)
43 return;
44
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!
48 const FileEntry *FE =
49 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
50 if (!FE)
51 return;
52
53 StringRef Filename = FE->getName();
54
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);
61 }
62
63 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
64 FileType != SrcMgr::C_User,
65 /*IsModuleFile*/false, /*IsMissing*/false);
66 }
67
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 {
73 if (!File)
74 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
75 /*IsSystem*/false, /*IsModuleFile*/false,
76 /*IsMissing*/true);
77 // Files that actually exist are handled by FileChanged.
78 }
79
80 void EndOfMainFile() override {
81 DepCollector.finishedMainFile();
82 }
83};
84
85struct 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();
91 }
92 void visitModuleFile(StringRef Filename) override {
93 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
94 /*IsSystem*/false, /*IsModuleFile*/true,
95 /*IsMissing*/false);
96 }
97 bool visitInputFile(StringRef Filename, bool IsSystem,
98 bool IsOverridden) override {
99 if (IsOverridden)
100 return true;
101
102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
103 /*IsModuleFile*/false, /*IsMissing*/false);
104 return true;
105 }
106};
107} // end anonymous namespace
108
109void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
110 bool IsSystem, bool IsModuleFile,
111 bool IsMissing) {
Stephen Hines176edba2014-12-01 14:53:08 -0800112 if (Seen.insert(Filename).second &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700113 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
114 Dependencies.push_back(Filename);
115}
116
Stephen Hines176edba2014-12-01 14:53:08 -0800117static bool isSpecialFilename(StringRef Filename) {
118 return llvm::StringSwitch<bool>(Filename)
119 .Case("<built-in>", true)
120 .Case("<stdin>", true)
121 .Default(false);
122}
123
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700124bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
125 bool IsSystem, bool IsModuleFile,
126 bool IsMissing) {
Stephen Hines176edba2014-12-01 14:53:08 -0800127 return !isSpecialFilename(Filename) &&
128 (needSystemDependencies() || !IsSystem);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700129}
130
131DependencyCollector::~DependencyCollector() { }
132void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Stephen Hines176edba2014-12-01 14:53:08 -0800133 PP.addPPCallbacks(
134 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700135}
136void DependencyCollector::attachToASTReader(ASTReader &R) {
Stephen Hines176edba2014-12-01 14:53:08 -0800137 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700138}
139
140namespace {
Stephen Hines651f13c2014-04-23 16:59:28 -0700141/// Private implementation for DependencyFileGenerator
142class DFGImpl : public PPCallbacks {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000143 std::vector<std::string> Files;
144 llvm::StringSet<> FilesSet;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000145 const Preprocessor *PP;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000146 std::string OutputFile;
Chris Lattner02fbb252009-01-11 19:28:34 +0000147 std::vector<std::string> Targets;
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000148 bool IncludeSystemHeaders;
149 bool PhonyTarget;
Peter Collingbournebb527862011-07-12 19:35:15 +0000150 bool AddMissingHeaderDeps;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000151 bool SeenMissingHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -0700152 bool IncludeModuleFiles;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000153private:
154 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner9d728512008-10-27 01:19:25 +0000155 SrcMgr::CharacteristicKind FileType);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000156 void OutputDependencyFile();
157
158public:
Stephen Hines651f13c2014-04-23 16:59:28 -0700159 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000160 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000161 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbournebb527862011-07-12 19:35:15 +0000162 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000163 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Stephen Hines651f13c2014-04-23 16:59:28 -0700164 SeenMissingHeader(false),
165 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000166
Stephen Hines651f13c2014-04-23 16:59:28 -0700167 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
168 SrcMgr::CharacteristicKind FileType,
169 FileID PrevFID) override;
170 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
171 StringRef FileName, bool IsAngled,
172 CharSourceRange FilenameRange, const FileEntry *File,
173 StringRef SearchPath, StringRef RelativePath,
174 const Module *Imported) override;
Daniel Dunbardbd82092010-03-23 05:09:10 +0000175
Stephen Hines651f13c2014-04-23 16:59:28 -0700176 void EndOfMainFile() override {
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000177 OutputDependencyFile();
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000178 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700179
180 void AddFilename(StringRef Filename);
181 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
182 bool includeModuleFiles() const { return IncludeModuleFiles; }
183};
184
185class DFGASTReaderListener : public ASTReaderListener {
186 DFGImpl &Parent;
187public:
188 DFGASTReaderListener(DFGImpl &Parent)
189 : Parent(Parent) { }
190 bool needsInputFileVisitation() override { return true; }
191 bool needsSystemInputFileVisitation() override {
192 return Parent.includeSystemHeaders();
193 }
194 void visitModuleFile(StringRef Filename) override;
195 bool visitInputFile(StringRef Filename, bool isSystem,
196 bool isOverridden) override;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000197};
198}
199
Stephen Hines651f13c2014-04-23 16:59:28 -0700200DependencyFileGenerator::DependencyFileGenerator(void *Impl)
201: Impl(Impl) { }
202
203DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
204 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
205
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000206 if (Opts.Targets.empty()) {
Daniel Dunbarca11f612009-11-11 21:44:00 +0000207 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700208 return nullptr;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000209 }
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000210
Peter Collingbournebb527862011-07-12 19:35:15 +0000211 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedmanf84139a2011-08-30 23:07:51 +0000212 if (Opts.AddMissingHeaderDeps)
213 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbournebb527862011-07-12 19:35:15 +0000214
Stephen Hines651f13c2014-04-23 16:59:28 -0700215 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Stephen Hines176edba2014-12-01 14:53:08 -0800216 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Stephen Hines651f13c2014-04-23 16:59:28 -0700217 return new DependencyFileGenerator(Callback);
218}
219
220void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
221 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
222 assert(I && "missing implementation");
Stephen Hines176edba2014-12-01 14:53:08 -0800223 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbar750c3582008-10-24 22:12:41 +0000224}
225
226/// FileMatchesDepCriteria - Determine whether the given Filename should be
227/// considered as a dependency.
Stephen Hines651f13c2014-04-23 16:59:28 -0700228bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
229 SrcMgr::CharacteristicKind FileType) {
Stephen Hines176edba2014-12-01 14:53:08 -0800230 if (isSpecialFilename(Filename))
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000231 return false;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000232
Eli Friedmanb5c8f8b2009-05-19 03:35:57 +0000233 if (IncludeSystemHeaders)
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000234 return true;
235
236 return FileType == SrcMgr::C_User;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000237}
238
Stephen Hines651f13c2014-04-23 16:59:28 -0700239void DFGImpl::FileChanged(SourceLocation Loc,
240 FileChangeReason Reason,
241 SrcMgr::CharacteristicKind FileType,
242 FileID PrevFID) {
Daniel Dunbar750c3582008-10-24 22:12:41 +0000243 if (Reason != PPCallbacks::EnterFile)
244 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Daniel Dunbara5a7bd02009-03-30 00:34:04 +0000246 // Dependency generation really does want to go all the way to the
247 // file entry for a source location to find out what is depended on.
248 // We do not want #line markers to affect dependency generation!
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000249 SourceManager &SM = PP->getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattnere86e4cd02009-01-28 05:42:38 +0000251 const FileEntry *FE =
Chandler Carruth40278532011-07-25 16:49:02 +0000252 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700253 if (!FE) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 StringRef Filename = FE->getName();
Eli Friedmana6e023c2011-07-08 20:17:28 +0000256 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbar750c3582008-10-24 22:12:41 +0000257 return;
258
Eli Friedmana6e023c2011-07-08 20:17:28 +0000259 // Remove leading "./" (or ".//" or "././" etc.)
260 while (Filename.size() > 2 && Filename[0] == '.' &&
261 llvm::sys::path::is_separator(Filename[1])) {
262 Filename = Filename.substr(1);
263 while (llvm::sys::path::is_separator(Filename[0]))
264 Filename = Filename.substr(1);
265 }
266
Peter Collingbournebb527862011-07-12 19:35:15 +0000267 AddFilename(Filename);
268}
Daniel Dunbar750c3582008-10-24 22:12:41 +0000269
Stephen Hines651f13c2014-04-23 16:59:28 -0700270void DFGImpl::InclusionDirective(SourceLocation HashLoc,
271 const Token &IncludeTok,
272 StringRef FileName,
273 bool IsAngled,
274 CharSourceRange FilenameRange,
275 const FileEntry *File,
276 StringRef SearchPath,
277 StringRef RelativePath,
278 const Module *Imported) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000279 if (!File) {
280 if (AddMissingHeaderDeps)
281 AddFilename(FileName);
282 else
283 SeenMissingHeader = true;
284 }
Peter Collingbournebb527862011-07-12 19:35:15 +0000285}
286
Stephen Hines651f13c2014-04-23 16:59:28 -0700287void DFGImpl::AddFilename(StringRef Filename) {
Stephen Hines176edba2014-12-01 14:53:08 -0800288 if (FilesSet.insert(Filename).second)
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000289 Files.push_back(Filename);
Daniel Dunbar750c3582008-10-24 22:12:41 +0000290}
291
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000292/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
293/// other scary characters.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner9d506342011-02-17 02:14:49 +0000295 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000296 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner9d506342011-02-17 02:14:49 +0000297 OS << '\\';
Benjamin Kramerddc15c42013-04-02 13:38:48 +0000298 else if (Filename[i] == '$') // $ is escaped by $$.
299 OS << '$';
Chris Lattner9d506342011-02-17 02:14:49 +0000300 OS << Filename[i];
301 }
302}
303
Stephen Hines651f13c2014-04-23 16:59:28 -0700304void DFGImpl::OutputDependencyFile() {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000305 if (SeenMissingHeader) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700306 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000307 return;
308 }
309
Stephen Hines176edba2014-12-01 14:53:08 -0800310 std::error_code EC;
311 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
312 if (EC) {
313 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
314 << EC.message();
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000315 return;
316 }
317
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000318 // Write out the dependency targets, trying to avoid overly long
319 // lines when possible. We try our best to emit exactly the same
320 // dependency file as GCC (4.2), assuming the included files are the
321 // same.
322 const unsigned MaxColumns = 75;
Chris Lattner02fbb252009-01-11 19:28:34 +0000323 unsigned Columns = 0;
324
325 for (std::vector<std::string>::iterator
326 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
327 unsigned N = I->length();
328 if (Columns == 0) {
329 Columns += N;
Chris Lattner02fbb252009-01-11 19:28:34 +0000330 } else if (Columns + N + 2 > MaxColumns) {
331 Columns = N + 2;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000332 OS << " \\\n ";
Chris Lattner02fbb252009-01-11 19:28:34 +0000333 } else {
334 Columns += N + 1;
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000335 OS << ' ';
Chris Lattner02fbb252009-01-11 19:28:34 +0000336 }
Chris Lattner9d506342011-02-17 02:14:49 +0000337 // Targets already quoted as needed.
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000338 OS << *I;
Chris Lattner02fbb252009-01-11 19:28:34 +0000339 }
340
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000341 OS << ':';
Chris Lattner02fbb252009-01-11 19:28:34 +0000342 Columns += 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000344 // Now add each dependency in the order it was seen, but avoiding
345 // duplicates.
346 for (std::vector<std::string>::iterator I = Files.begin(),
347 E = Files.end(); I != E; ++I) {
348 // Start a new line if this would exceed the column limit. Make
349 // sure to leave space for a trailing " \" in case we need to
350 // break the line on the next iteration.
351 unsigned N = I->length();
352 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000353 OS << " \\\n ";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000354 Columns = 2;
355 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000356 OS << ' ';
357 PrintFilename(OS, *I);
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000358 Columns += N + 1;
Daniel Dunbar750c3582008-10-24 22:12:41 +0000359 }
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000360 OS << '\n';
Daniel Dunbar750c3582008-10-24 22:12:41 +0000361
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000362 // Create phony targets if requested.
Fariborz Jahanianccad3db2011-04-15 18:49:23 +0000363 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000364 // Skip the first entry, this is always the input file itself.
365 for (std::vector<std::string>::iterator I = Files.begin() + 1,
366 E = Files.end(); I != E; ++I) {
Peter Collingbourne1b91ab42011-11-21 00:01:14 +0000367 OS << '\n';
368 PrintFilename(OS, *I);
369 OS << ":\n";
Daniel Dunbar7e9f1f72008-10-27 20:01:06 +0000370 }
371 }
Daniel Dunbar750c3582008-10-24 22:12:41 +0000372}
373
Stephen Hines651f13c2014-04-23 16:59:28 -0700374bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
375 bool IsSystem, bool IsOverridden) {
376 assert(!IsSystem || needsSystemInputFileVisitation());
377 if (IsOverridden)
378 return true;
379
380 Parent.AddFilename(Filename);
381 return true;
382}
383
384void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
385 if (Parent.includeModuleFiles())
386 Parent.AddFilename(Filename);
387}