blob: 67a24c426296c10b2c73418def1e08dffa403afe [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"
Richard Smith2a6edb32015-08-09 04:46:57 +000021#include "clang/Lex/ModuleMap.h"
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +000022#include "clang/Lex/PPCallbacks.h"
23#include "clang/Lex/Preprocessor.h"
Ben Langmuircb69b572014-03-07 06:40:32 +000024#include "clang/Serialization/ASTReader.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000025#include "llvm/ADT/StringSet.h"
David Majnemerf0822fb2014-10-27 22:31:50 +000026#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000027#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000028#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000030
31using namespace clang;
32
33namespace {
Ben Langmuir33c80902014-06-30 20:04:14 +000034struct DepCollectorPPCallbacks : public PPCallbacks {
35 DependencyCollector &DepCollector;
36 SourceManager &SM;
37 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
38 : DepCollector(L), SM(SM) { }
39
40 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
41 SrcMgr::CharacteristicKind FileType,
42 FileID PrevFID) override {
43 if (Reason != PPCallbacks::EnterFile)
44 return;
45
46 // Dependency generation really does want to go all the way to the
47 // file entry for a source location to find out what is depended on.
48 // We do not want #line markers to affect dependency generation!
49 const FileEntry *FE =
50 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
51 if (!FE)
52 return;
53
54 StringRef Filename = FE->getName();
55
56 // Remove leading "./" (or ".//" or "././" etc.)
57 while (Filename.size() > 2 && Filename[0] == '.' &&
58 llvm::sys::path::is_separator(Filename[1])) {
59 Filename = Filename.substr(1);
60 while (llvm::sys::path::is_separator(Filename[0]))
61 Filename = Filename.substr(1);
62 }
63
64 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
65 FileType != SrcMgr::C_User,
66 /*IsModuleFile*/false, /*IsMissing*/false);
67 }
68
69 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
70 StringRef FileName, bool IsAngled,
71 CharSourceRange FilenameRange, const FileEntry *File,
72 StringRef SearchPath, StringRef RelativePath,
73 const Module *Imported) override {
74 if (!File)
75 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
76 /*IsSystem*/false, /*IsModuleFile*/false,
77 /*IsMissing*/true);
78 // Files that actually exist are handled by FileChanged.
79 }
80
81 void EndOfMainFile() override {
82 DepCollector.finishedMainFile();
83 }
84};
85
Richard Smith2a6edb32015-08-09 04:46:57 +000086struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
87 DependencyCollector &DepCollector;
88 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
89
90 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
91 bool IsSystem) override {
92 StringRef Filename = Entry.getName();
93 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
94 /*IsSystem*/IsSystem,
95 /*IsModuleFile*/false,
96 /*IsMissing*/false);
97 }
98};
99
Ben Langmuir33c80902014-06-30 20:04:14 +0000100struct DepCollectorASTListener : public ASTReaderListener {
101 DependencyCollector &DepCollector;
102 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
103 bool needsInputFileVisitation() override { return true; }
104 bool needsSystemInputFileVisitation() override {
105 return DepCollector.needSystemDependencies();
106 }
107 void visitModuleFile(StringRef Filename) override {
108 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
109 /*IsSystem*/false, /*IsModuleFile*/true,
110 /*IsMissing*/false);
111 }
112 bool visitInputFile(StringRef Filename, bool IsSystem,
113 bool IsOverridden) override {
114 if (IsOverridden)
115 return true;
116
117 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
118 /*IsModuleFile*/false, /*IsMissing*/false);
119 return true;
120 }
121};
122} // end anonymous namespace
123
124void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
125 bool IsSystem, bool IsModuleFile,
126 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000127 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000128 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
129 Dependencies.push_back(Filename);
130}
131
David Majnemerf0822fb2014-10-27 22:31:50 +0000132static bool isSpecialFilename(StringRef Filename) {
133 return llvm::StringSwitch<bool>(Filename)
134 .Case("<built-in>", true)
135 .Case("<stdin>", true)
136 .Default(false);
137}
138
Ben Langmuir33c80902014-06-30 20:04:14 +0000139bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
140 bool IsSystem, bool IsModuleFile,
141 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000142 return !isSpecialFilename(Filename) &&
143 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000144}
145
146DependencyCollector::~DependencyCollector() { }
147void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000148 PP.addPPCallbacks(
149 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000150 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
151 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000152}
153void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000154 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000155}
156
157namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000158/// Private implementation for DependencyFileGenerator
159class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000160 std::vector<std::string> Files;
161 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000162 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000163 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000164 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000165 bool IncludeSystemHeaders;
166 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000167 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000168 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000169 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000170 DependencyOutputFormat OutputFormat;
171
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000172private:
173 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000174 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000175 void OutputDependencyFile();
176
177public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000178 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000179 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000180 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000181 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000182 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000183 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000184 IncludeModuleFiles(Opts.IncludeModuleFiles),
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000185 OutputFormat(Opts.OutputFormat) {
186 for (auto ExtraDep : Opts.ExtraDeps) {
187 AddFilename(ExtraDep);
188 }
189 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000190
Craig Topperafa7cb32014-03-13 06:07:04 +0000191 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
192 SrcMgr::CharacteristicKind FileType,
193 FileID PrevFID) override;
194 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
195 StringRef FileName, bool IsAngled,
196 CharSourceRange FilenameRange, const FileEntry *File,
197 StringRef SearchPath, StringRef RelativePath,
198 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000199
Craig Topperafa7cb32014-03-13 06:07:04 +0000200 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000201 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000202 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000203
204 void AddFilename(StringRef Filename);
205 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000206 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000207};
208
Richard Smith2a6edb32015-08-09 04:46:57 +0000209class DFGMMCallback : public ModuleMapCallbacks {
210 DFGImpl &Parent;
211public:
212 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
213 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
214 bool IsSystem) override {
215 if (!IsSystem || Parent.includeSystemHeaders())
216 Parent.AddFilename(Entry.getName());
217 }
218};
219
Ben Langmuircb69b572014-03-07 06:40:32 +0000220class DFGASTReaderListener : public ASTReaderListener {
221 DFGImpl &Parent;
222public:
223 DFGASTReaderListener(DFGImpl &Parent)
224 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000225 bool needsInputFileVisitation() override { return true; }
226 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000227 return Parent.includeSystemHeaders();
228 }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000229 void visitModuleFile(StringRef Filename) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000230 bool visitInputFile(StringRef Filename, bool isSystem,
231 bool isOverridden) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000232};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000233}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000234
Ben Langmuircb69b572014-03-07 06:40:32 +0000235DependencyFileGenerator::DependencyFileGenerator(void *Impl)
236: Impl(Impl) { }
237
238DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
239 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
240
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000241 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000242 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000243 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000244 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000245
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000246 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000247 if (Opts.AddMissingHeaderDeps)
248 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000249
Ben Langmuircb69b572014-03-07 06:40:32 +0000250 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000251 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000252 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
253 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000254 return new DependencyFileGenerator(Callback);
255}
256
257void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
258 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
259 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000260 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000261}
262
263/// FileMatchesDepCriteria - Determine whether the given Filename should be
264/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000265bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
266 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000267 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000268 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000269
Eli Friedman33cd03c2009-05-19 03:35:57 +0000270 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000271 return true;
272
273 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000274}
275
Ben Langmuircb69b572014-03-07 06:40:32 +0000276void DFGImpl::FileChanged(SourceLocation Loc,
277 FileChangeReason Reason,
278 SrcMgr::CharacteristicKind FileType,
279 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000280 if (Reason != PPCallbacks::EnterFile)
281 return;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000283 // Dependency generation really does want to go all the way to the
284 // file entry for a source location to find out what is depended on.
285 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000286 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattner64d8fc22009-01-28 05:42:38 +0000288 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000289 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000290 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000292 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000293 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000294 return;
295
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000296 // Remove leading "./" (or ".//" or "././" etc.)
297 while (Filename.size() > 2 && Filename[0] == '.' &&
298 llvm::sys::path::is_separator(Filename[1])) {
299 Filename = Filename.substr(1);
300 while (llvm::sys::path::is_separator(Filename[0]))
301 Filename = Filename.substr(1);
302 }
303
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000304 AddFilename(Filename);
305}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000306
Ben Langmuircb69b572014-03-07 06:40:32 +0000307void DFGImpl::InclusionDirective(SourceLocation HashLoc,
308 const Token &IncludeTok,
309 StringRef FileName,
310 bool IsAngled,
311 CharSourceRange FilenameRange,
312 const FileEntry *File,
313 StringRef SearchPath,
314 StringRef RelativePath,
315 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000316 if (!File) {
317 if (AddMissingHeaderDeps)
318 AddFilename(FileName);
319 else
320 SeenMissingHeader = true;
321 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000322}
323
Ben Langmuircb69b572014-03-07 06:40:32 +0000324void DFGImpl::AddFilename(StringRef Filename) {
David Blaikie61b86d42014-11-19 02:56:13 +0000325 if (FilesSet.insert(Filename).second)
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000326 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000327}
328
Paul Robinson64441de2015-05-13 21:18:15 +0000329/// Print the filename, with escaping or quoting that accommodates the three
330/// most likely tools that use dependency files: GNU Make, BSD Make, and
331/// NMake/Jom.
332///
333/// BSD Make is the simplest case: It does no escaping at all. This means
334/// characters that are normally delimiters, i.e. space and # (the comment
335/// character) simply aren't supported in filenames.
336///
337/// GNU Make does allow space and # in filenames, but to avoid being treated
338/// as a delimiter or comment, these must be escaped with a backslash. Because
339/// backslash is itself the escape character, if a backslash appears in a
340/// filename, it should be escaped as well. (As a special case, $ is escaped
341/// as $$, which is the normal Make way to handle the $ character.)
342/// For compatibility with BSD Make and historical practice, if GNU Make
343/// un-escapes characters in a filename but doesn't find a match, it will
344/// retry with the unmodified original string.
345///
346/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000347/// characters in the original filename, but not escaping backslashes. The
348/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000349/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
350/// unmodified original string; filenames with # or space characters aren't
351/// supported by BSD Make at all, but will be handled correctly by GNU Make
352/// due to the escaping.
353///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000354/// A corner case that GCC gets only partly right is when the original filename
355/// has a backslash immediately followed by space or #. GNU Make would expect
356/// this backslash to be escaped; however GCC escapes the original backslash
357/// only when followed by space, not #. It will therefore take a dependency
358/// from a directive such as
359/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000360/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000361/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000362/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000363/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000364/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000365/// original string, which probably doesn't exist either; in any case it won't
366/// find
367/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000368/// which is the actual filename specified by the include directive.
369///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000370/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000371///
372/// NMake/Jom has a different set of scary characters, but wraps filespecs in
373/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000374/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
375/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
376/// for Windows file-naming info.
377static void PrintFilename(raw_ostream &OS, StringRef Filename,
378 DependencyOutputFormat OutputFormat) {
379 if (OutputFormat == DependencyOutputFormat::NMake) {
380 // Add quotes if needed. These are the characters listed as "special" to
381 // NMake, that are legal in a Windows filespec, and that could cause
382 // misinterpretation of the dependency string.
383 if (Filename.find_first_of(" #${}^!") != StringRef::npos)
384 OS << '\"' << Filename << '\"';
385 else
386 OS << Filename;
387 return;
388 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000389 assert(OutputFormat == DependencyOutputFormat::Make);
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000390 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000391 if (Filename[i] == '#') // Handle '#' the broken gcc way.
392 OS << '\\';
393 else if (Filename[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000394 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000395 unsigned j = i;
396 while (j > 0 && Filename[--j] == '\\')
397 OS << '\\';
398 } else if (Filename[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000399 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000400 OS << Filename[i];
401 }
402}
403
Ben Langmuircb69b572014-03-07 06:40:32 +0000404void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000405 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000406 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000407 return;
408 }
409
Rafael Espindoladae941a2014-08-25 18:17:04 +0000410 std::error_code EC;
411 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
412 if (EC) {
413 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
414 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000415 return;
416 }
417
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000418 // Write out the dependency targets, trying to avoid overly long
419 // lines when possible. We try our best to emit exactly the same
420 // dependency file as GCC (4.2), assuming the included files are the
421 // same.
422 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000423 unsigned Columns = 0;
424
425 for (std::vector<std::string>::iterator
426 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
427 unsigned N = I->length();
428 if (Columns == 0) {
429 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000430 } else if (Columns + N + 2 > MaxColumns) {
431 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000432 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000433 } else {
434 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000435 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000436 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000437 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000438 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000439 }
440
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000441 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000442 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000444 // Now add each dependency in the order it was seen, but avoiding
445 // duplicates.
446 for (std::vector<std::string>::iterator I = Files.begin(),
447 E = Files.end(); I != E; ++I) {
448 // Start a new line if this would exceed the column limit. Make
449 // sure to leave space for a trailing " \" in case we need to
450 // break the line on the next iteration.
451 unsigned N = I->length();
452 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000453 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000454 Columns = 2;
455 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000456 OS << ' ';
Paul Robinsond7214a72015-04-27 18:14:32 +0000457 PrintFilename(OS, *I, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000458 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000459 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000460 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000461
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000462 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000463 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000464 // Skip the first entry, this is always the input file itself.
465 for (std::vector<std::string>::iterator I = Files.begin() + 1,
466 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000467 OS << '\n';
Paul Robinsond7214a72015-04-27 18:14:32 +0000468 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000469 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000470 }
471 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000472}
473
Ben Langmuircb69b572014-03-07 06:40:32 +0000474bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000475 bool IsSystem, bool IsOverridden) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000476 assert(!IsSystem || needsSystemInputFileVisitation());
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000477 if (IsOverridden)
478 return true;
479
Ben Langmuircb69b572014-03-07 06:40:32 +0000480 Parent.AddFilename(Filename);
481 return true;
482}
483
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000484void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) {
485 if (Parent.includeModuleFiles())
486 Parent.AddFilename(Filename);
487}