blob: c5c34490d71e95fb84ca5fc66cbae0456701a953 [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 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000107 void visitModuleFile(StringRef Filename,
108 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000109 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
110 /*IsSystem*/false, /*IsModuleFile*/true,
111 /*IsMissing*/false);
112 }
113 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000114 bool IsOverridden, bool IsExplicitModule) override {
115 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000116 return true;
117
118 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
119 /*IsModuleFile*/false, /*IsMissing*/false);
120 return true;
121 }
122};
123} // end anonymous namespace
124
125void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
126 bool IsSystem, bool IsModuleFile,
127 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000128 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000129 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
130 Dependencies.push_back(Filename);
131}
132
David Majnemerf0822fb2014-10-27 22:31:50 +0000133static bool isSpecialFilename(StringRef Filename) {
134 return llvm::StringSwitch<bool>(Filename)
135 .Case("<built-in>", true)
136 .Case("<stdin>", true)
137 .Default(false);
138}
139
Ben Langmuir33c80902014-06-30 20:04:14 +0000140bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
141 bool IsSystem, bool IsModuleFile,
142 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000143 return !isSpecialFilename(Filename) &&
144 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000145}
146
147DependencyCollector::~DependencyCollector() { }
148void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000149 PP.addPPCallbacks(
150 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000151 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
152 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000153}
154void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000155 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000156}
157
158namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000159/// Private implementation for DependencyFileGenerator
160class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000161 std::vector<std::string> Files;
162 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000163 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000164 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000165 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000166 bool IncludeSystemHeaders;
167 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000168 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000169 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000170 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000171 DependencyOutputFormat OutputFormat;
172
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000173private:
174 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000175 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000176 void OutputDependencyFile();
177
178public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000179 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000180 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000181 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000182 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000183 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000184 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000185 IncludeModuleFiles(Opts.IncludeModuleFiles),
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000186 OutputFormat(Opts.OutputFormat) {
187 for (auto ExtraDep : Opts.ExtraDeps) {
188 AddFilename(ExtraDep);
189 }
190 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000191
Craig Topperafa7cb32014-03-13 06:07:04 +0000192 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
193 SrcMgr::CharacteristicKind FileType,
194 FileID PrevFID) override;
195 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
196 StringRef FileName, bool IsAngled,
197 CharSourceRange FilenameRange, const FileEntry *File,
198 StringRef SearchPath, StringRef RelativePath,
199 const Module *Imported) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000200
Craig Topperafa7cb32014-03-13 06:07:04 +0000201 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000202 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000203 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000204
205 void AddFilename(StringRef Filename);
206 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000207 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000208};
209
Richard Smith2a6edb32015-08-09 04:46:57 +0000210class DFGMMCallback : public ModuleMapCallbacks {
211 DFGImpl &Parent;
212public:
213 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
214 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
215 bool IsSystem) override {
216 if (!IsSystem || Parent.includeSystemHeaders())
217 Parent.AddFilename(Entry.getName());
218 }
219};
220
Ben Langmuircb69b572014-03-07 06:40:32 +0000221class DFGASTReaderListener : public ASTReaderListener {
222 DFGImpl &Parent;
223public:
224 DFGASTReaderListener(DFGImpl &Parent)
225 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000226 bool needsInputFileVisitation() override { return true; }
227 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000228 return Parent.includeSystemHeaders();
229 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000230 void visitModuleFile(StringRef Filename,
231 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000232 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000233 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000234};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000235}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000236
Ben Langmuircb69b572014-03-07 06:40:32 +0000237DependencyFileGenerator::DependencyFileGenerator(void *Impl)
238: Impl(Impl) { }
239
240DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
241 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
242
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000243 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000244 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000245 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000246 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000247
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000248 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000249 if (Opts.AddMissingHeaderDeps)
250 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000251
Ben Langmuircb69b572014-03-07 06:40:32 +0000252 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000253 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000254 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
255 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000256 return new DependencyFileGenerator(Callback);
257}
258
259void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
260 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
261 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000262 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000263}
264
265/// FileMatchesDepCriteria - Determine whether the given Filename should be
266/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000267bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
268 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000269 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000270 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000271
Eli Friedman33cd03c2009-05-19 03:35:57 +0000272 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000273 return true;
274
275 return FileType == SrcMgr::C_User;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000276}
277
Ben Langmuircb69b572014-03-07 06:40:32 +0000278void DFGImpl::FileChanged(SourceLocation Loc,
279 FileChangeReason Reason,
280 SrcMgr::CharacteristicKind FileType,
281 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000282 if (Reason != PPCallbacks::EnterFile)
283 return;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000285 // Dependency generation really does want to go all the way to the
286 // file entry for a source location to find out what is depended on.
287 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000288 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattner64d8fc22009-01-28 05:42:38 +0000290 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000291 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000292 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000294 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000295 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000296 return;
297
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000298 // Remove leading "./" (or ".//" or "././" etc.)
299 while (Filename.size() > 2 && Filename[0] == '.' &&
300 llvm::sys::path::is_separator(Filename[1])) {
301 Filename = Filename.substr(1);
302 while (llvm::sys::path::is_separator(Filename[0]))
303 Filename = Filename.substr(1);
304 }
305
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000306 AddFilename(Filename);
307}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000308
Ben Langmuircb69b572014-03-07 06:40:32 +0000309void DFGImpl::InclusionDirective(SourceLocation HashLoc,
310 const Token &IncludeTok,
311 StringRef FileName,
312 bool IsAngled,
313 CharSourceRange FilenameRange,
314 const FileEntry *File,
315 StringRef SearchPath,
316 StringRef RelativePath,
317 const Module *Imported) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000318 if (!File) {
319 if (AddMissingHeaderDeps)
320 AddFilename(FileName);
321 else
322 SeenMissingHeader = true;
323 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000324}
325
Ben Langmuircb69b572014-03-07 06:40:32 +0000326void DFGImpl::AddFilename(StringRef Filename) {
David Blaikie61b86d42014-11-19 02:56:13 +0000327 if (FilesSet.insert(Filename).second)
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000328 Files.push_back(Filename);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000329}
330
Paul Robinson64441de2015-05-13 21:18:15 +0000331/// Print the filename, with escaping or quoting that accommodates the three
332/// most likely tools that use dependency files: GNU Make, BSD Make, and
333/// NMake/Jom.
334///
335/// BSD Make is the simplest case: It does no escaping at all. This means
336/// characters that are normally delimiters, i.e. space and # (the comment
337/// character) simply aren't supported in filenames.
338///
339/// GNU Make does allow space and # in filenames, but to avoid being treated
340/// as a delimiter or comment, these must be escaped with a backslash. Because
341/// backslash is itself the escape character, if a backslash appears in a
342/// filename, it should be escaped as well. (As a special case, $ is escaped
343/// as $$, which is the normal Make way to handle the $ character.)
344/// For compatibility with BSD Make and historical practice, if GNU Make
345/// un-escapes characters in a filename but doesn't find a match, it will
346/// retry with the unmodified original string.
347///
348/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000349/// characters in the original filename, but not escaping backslashes. The
350/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000351/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
352/// unmodified original string; filenames with # or space characters aren't
353/// supported by BSD Make at all, but will be handled correctly by GNU Make
354/// due to the escaping.
355///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000356/// A corner case that GCC gets only partly right is when the original filename
357/// has a backslash immediately followed by space or #. GNU Make would expect
358/// this backslash to be escaped; however GCC escapes the original backslash
359/// only when followed by space, not #. It will therefore take a dependency
360/// from a directive such as
361/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000362/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000363/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000364/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000365/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000366/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000367/// original string, which probably doesn't exist either; in any case it won't
368/// find
369/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000370/// which is the actual filename specified by the include directive.
371///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000372/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000373///
374/// NMake/Jom has a different set of scary characters, but wraps filespecs in
375/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000376/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
377/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
378/// for Windows file-naming info.
379static void PrintFilename(raw_ostream &OS, StringRef Filename,
380 DependencyOutputFormat OutputFormat) {
381 if (OutputFormat == DependencyOutputFormat::NMake) {
382 // Add quotes if needed. These are the characters listed as "special" to
383 // NMake, that are legal in a Windows filespec, and that could cause
384 // misinterpretation of the dependency string.
385 if (Filename.find_first_of(" #${}^!") != StringRef::npos)
386 OS << '\"' << Filename << '\"';
387 else
388 OS << Filename;
389 return;
390 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000391 assert(OutputFormat == DependencyOutputFormat::Make);
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000392 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000393 if (Filename[i] == '#') // Handle '#' the broken gcc way.
394 OS << '\\';
395 else if (Filename[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000396 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000397 unsigned j = i;
398 while (j > 0 && Filename[--j] == '\\')
399 OS << '\\';
400 } else if (Filename[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000401 OS << '$';
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000402 OS << Filename[i];
403 }
404}
405
Ben Langmuircb69b572014-03-07 06:40:32 +0000406void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000407 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000408 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000409 return;
410 }
411
Rafael Espindoladae941a2014-08-25 18:17:04 +0000412 std::error_code EC;
413 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
414 if (EC) {
415 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
416 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000417 return;
418 }
419
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000420 // Write out the dependency targets, trying to avoid overly long
421 // lines when possible. We try our best to emit exactly the same
422 // dependency file as GCC (4.2), assuming the included files are the
423 // same.
424 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000425 unsigned Columns = 0;
426
427 for (std::vector<std::string>::iterator
428 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
429 unsigned N = I->length();
430 if (Columns == 0) {
431 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000432 } else if (Columns + N + 2 > MaxColumns) {
433 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000434 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000435 } else {
436 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000437 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000438 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000439 // Targets already quoted as needed.
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000440 OS << *I;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000441 }
442
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000443 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000444 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000446 // Now add each dependency in the order it was seen, but avoiding
447 // duplicates.
448 for (std::vector<std::string>::iterator I = Files.begin(),
449 E = Files.end(); I != E; ++I) {
450 // Start a new line if this would exceed the column limit. Make
451 // sure to leave space for a trailing " \" in case we need to
452 // break the line on the next iteration.
453 unsigned N = I->length();
454 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000455 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000456 Columns = 2;
457 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000458 OS << ' ';
Paul Robinsond7214a72015-04-27 18:14:32 +0000459 PrintFilename(OS, *I, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000460 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000461 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000462 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000463
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000464 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000465 if (PhonyTarget && !Files.empty()) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000466 // Skip the first entry, this is always the input file itself.
467 for (std::vector<std::string>::iterator I = Files.begin() + 1,
468 E = Files.end(); I != E; ++I) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000469 OS << '\n';
Paul Robinsond7214a72015-04-27 18:14:32 +0000470 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000471 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000472 }
473 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000474}
475
Ben Langmuircb69b572014-03-07 06:40:32 +0000476bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000477 bool IsSystem, bool IsOverridden,
478 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000479 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000480 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000481 return true;
482
Ben Langmuircb69b572014-03-07 06:40:32 +0000483 Parent.AddFilename(Filename);
484 return true;
485}
486
Richard Smith216a3bd2015-08-13 17:57:10 +0000487void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
488 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000489 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000490 Parent.AddFilename(Filename);
491}