blob: 22ed38fc129f7441a9bc4ede35cb08784ecfe654 [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) {
Craig Topperb8a70532014-09-10 04:53:53 +0000124 PP.addPPCallbacks(
125 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Ben Langmuir33c80902014-06-30 20:04:14 +0000126}
127void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000128 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000129}
130
131namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000132/// Private implementation for DependencyFileGenerator
133class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000134 std::vector<std::string> Files;
135 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000136 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000137 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000138 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000139 bool IncludeSystemHeaders;
140 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000141 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000142 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000143 bool IncludeModuleFiles;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000144private:
145 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000146 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000147 void OutputDependencyFile();
148
149public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000150 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000151 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000152 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000153 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000154 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000155 SeenMissingHeader(false),
156 IncludeModuleFiles(Opts.IncludeModuleFiles) {}
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000157
Craig Topperafa7cb32014-03-13 06:07:04 +0000158 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
159 SrcMgr::CharacteristicKind FileType,
160 FileID PrevFID) override;
161 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
162 StringRef FileName, bool IsAngled,
163 CharSourceRange FilenameRange, const FileEntry *File,
164 StringRef SearchPath, StringRef RelativePath,
165 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000166
Craig Topperafa7cb32014-03-13 06:07:04 +0000167 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000168 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000169 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000170
171 void AddFilename(StringRef Filename);
172 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000173 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000174};
175
176class DFGASTReaderListener : public ASTReaderListener {
177 DFGImpl &Parent;
178public:
179 DFGASTReaderListener(DFGImpl &Parent)
180 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000181 bool needsInputFileVisitation() override { return true; }
182 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000183 return Parent.includeSystemHeaders();
184 }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000185 void visitModuleFile(StringRef Filename) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000186 bool visitInputFile(StringRef Filename, bool isSystem,
187 bool isOverridden) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000188};
189}
190
Ben Langmuircb69b572014-03-07 06:40:32 +0000191DependencyFileGenerator::DependencyFileGenerator(void *Impl)
192: Impl(Impl) { }
193
194DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
195 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
196
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000197 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000198 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000199 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000200 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000201
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000202 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000203 if (Opts.AddMissingHeaderDeps)
204 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000205
Ben Langmuircb69b572014-03-07 06:40:32 +0000206 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000207 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000208 return new DependencyFileGenerator(Callback);
209}
210
211void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
212 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
213 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000214 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000215}
216
217/// FileMatchesDepCriteria - Determine whether the given Filename should be
218/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000219bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
220 SrcMgr::CharacteristicKind FileType) {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000221 if (strcmp("<built-in>", Filename) == 0)
222 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000223
Eli Friedman33cd03c2009-05-19 03:35:57 +0000224 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000225 return true;
226
227 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000228}
229
Ben Langmuircb69b572014-03-07 06:40:32 +0000230void DFGImpl::FileChanged(SourceLocation Loc,
231 FileChangeReason Reason,
232 SrcMgr::CharacteristicKind FileType,
233 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000234 if (Reason != PPCallbacks::EnterFile)
235 return;
Mike Stump11289f42009-09-09 15:08:12 +0000236
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000237 // Dependency generation really does want to go all the way to the
238 // file entry for a source location to find out what is depended on.
239 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000240 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattner64d8fc22009-01-28 05:42:38 +0000242 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000243 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000244 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000246 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000247 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000248 return;
249
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000250 // Remove leading "./" (or ".//" or "././" etc.)
251 while (Filename.size() > 2 && Filename[0] == '.' &&
252 llvm::sys::path::is_separator(Filename[1])) {
253 Filename = Filename.substr(1);
254 while (llvm::sys::path::is_separator(Filename[0]))
255 Filename = Filename.substr(1);
256 }
257
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000258 AddFilename(Filename);
259}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000260
Ben Langmuircb69b572014-03-07 06:40:32 +0000261void DFGImpl::InclusionDirective(SourceLocation HashLoc,
262 const Token &IncludeTok,
263 StringRef FileName,
264 bool IsAngled,
265 CharSourceRange FilenameRange,
266 const FileEntry *File,
267 StringRef SearchPath,
268 StringRef RelativePath,
269 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000270 if (!File) {
271 if (AddMissingHeaderDeps)
272 AddFilename(FileName);
273 else
274 SeenMissingHeader = true;
275 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000276}
277
Ben Langmuircb69b572014-03-07 06:40:32 +0000278void DFGImpl::AddFilename(StringRef Filename) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000279 if (FilesSet.insert(Filename))
280 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000281}
282
Benjamin Kramerae611512013-04-02 13:38:48 +0000283/// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
284/// other scary characters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285static void PrintFilename(raw_ostream &OS, StringRef Filename) {
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000286 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Benjamin Kramerae611512013-04-02 13:38:48 +0000287 if (Filename[i] == ' ' || Filename[i] == '#')
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000288 OS << '\\';
Benjamin Kramerae611512013-04-02 13:38:48 +0000289 else if (Filename[i] == '$') // $ is escaped by $$.
290 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000291 OS << Filename[i];
292 }
293}
294
Ben Langmuircb69b572014-03-07 06:40:32 +0000295void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000296 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000297 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000298 return;
299 }
300
Rafael Espindoladae941a2014-08-25 18:17:04 +0000301 std::error_code EC;
302 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
303 if (EC) {
304 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
305 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000306 return;
307 }
308
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000309 // Write out the dependency targets, trying to avoid overly long
310 // lines when possible. We try our best to emit exactly the same
311 // dependency file as GCC (4.2), assuming the included files are the
312 // same.
313 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000314 unsigned Columns = 0;
315
316 for (std::vector<std::string>::iterator
317 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
318 unsigned N = I->length();
319 if (Columns == 0) {
320 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000321 } else if (Columns + N + 2 > MaxColumns) {
322 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000323 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000324 } else {
325 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000326 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000327 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000328 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000329 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000330 }
331
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000332 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000333 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000335 // Now add each dependency in the order it was seen, but avoiding
336 // duplicates.
337 for (std::vector<std::string>::iterator I = Files.begin(),
338 E = Files.end(); I != E; ++I) {
339 // Start a new line if this would exceed the column limit. Make
340 // sure to leave space for a trailing " \" in case we need to
341 // break the line on the next iteration.
342 unsigned N = I->length();
343 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000344 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000345 Columns = 2;
346 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000347 OS << ' ';
348 PrintFilename(OS, *I);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000349 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000350 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000351 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000352
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000353 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000354 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000355 // Skip the first entry, this is always the input file itself.
356 for (std::vector<std::string>::iterator I = Files.begin() + 1,
357 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000358 OS << '\n';
359 PrintFilename(OS, *I);
360 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000361 }
362 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000363}
364
Ben Langmuircb69b572014-03-07 06:40:32 +0000365bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000366 bool IsSystem, bool IsOverridden) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000367 assert(!IsSystem || needsSystemInputFileVisitation());
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000368 if (IsOverridden)
369 return true;
370
Ben Langmuircb69b572014-03-07 06:40:32 +0000371 Parent.AddFilename(Filename);
372 return true;
373}
374
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000375void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
376 if (Parent.includeModuleFiles())
377 Parent.AddFilename(Filename);
378}