blob: a3f347920918a7f36ae3ea8999e6b7d5f1b8da8b [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
Douglas Katzman7f43af52015-09-02 21:14:53 +000054 StringRef Filename =
55 llvm::sys::path::remove_leading_dotslash(FE->getName());
Ben Langmuir33c80902014-06-30 20:04:14 +000056
57 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
Richard Smithf3f84612017-06-29 02:19:42 +000058 isSystem(FileType),
59 /*IsModuleFile*/false, /*IsMissing*/false);
Ben Langmuir33c80902014-06-30 20:04:14 +000060 }
61
62 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
63 StringRef FileName, bool IsAngled,
64 CharSourceRange FilenameRange, const FileEntry *File,
65 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +000066 const Module *Imported,
67 SrcMgr::CharacteristicKind FileType) override {
Ben Langmuir33c80902014-06-30 20:04:14 +000068 if (!File)
69 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
70 /*IsSystem*/false, /*IsModuleFile*/false,
71 /*IsMissing*/true);
72 // Files that actually exist are handled by FileChanged.
73 }
74
75 void EndOfMainFile() override {
76 DepCollector.finishedMainFile();
77 }
78};
79
Richard Smith2a6edb32015-08-09 04:46:57 +000080struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
81 DependencyCollector &DepCollector;
82 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
83
84 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
85 bool IsSystem) override {
86 StringRef Filename = Entry.getName();
87 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
88 /*IsSystem*/IsSystem,
89 /*IsModuleFile*/false,
90 /*IsMissing*/false);
91 }
92};
93
Ben Langmuir33c80902014-06-30 20:04:14 +000094struct DepCollectorASTListener : public ASTReaderListener {
95 DependencyCollector &DepCollector;
96 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
97 bool needsInputFileVisitation() override { return true; }
98 bool needsSystemInputFileVisitation() override {
99 return DepCollector.needSystemDependencies();
100 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000101 void visitModuleFile(StringRef Filename,
102 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000103 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
104 /*IsSystem*/false, /*IsModuleFile*/true,
105 /*IsMissing*/false);
106 }
107 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000108 bool IsOverridden, bool IsExplicitModule) override {
109 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000110 return true;
111
112 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
113 /*IsModuleFile*/false, /*IsMissing*/false);
114 return true;
115 }
116};
117} // end anonymous namespace
118
119void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
120 bool IsSystem, bool IsModuleFile,
121 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000122 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000123 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
124 Dependencies.push_back(Filename);
125}
126
David Majnemerf0822fb2014-10-27 22:31:50 +0000127static bool isSpecialFilename(StringRef Filename) {
128 return llvm::StringSwitch<bool>(Filename)
129 .Case("<built-in>", true)
130 .Case("<stdin>", true)
131 .Default(false);
132}
133
Ben Langmuir33c80902014-06-30 20:04:14 +0000134bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
135 bool IsSystem, bool IsModuleFile,
136 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000137 return !isSpecialFilename(Filename) &&
138 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000139}
140
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000141DependencyCollector::~DependencyCollector() { }
Ben Langmuir33c80902014-06-30 20:04:14 +0000142void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000143 PP.addPPCallbacks(
144 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000145 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
146 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000147}
148void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000149 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000150}
151
152namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000153/// Private implementation for DependencyFileGenerator
154class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000155 std::vector<std::string> Files;
156 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000157 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000158 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000159 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000160 bool IncludeSystemHeaders;
161 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000162 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000163 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000164 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000165 DependencyOutputFormat OutputFormat;
David Stenbergd45d7ea2018-05-29 13:07:58 +0000166 unsigned InputFileIndex;
Paul Robinsond7214a72015-04-27 18:14:32 +0000167
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000168private:
169 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000170 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000171 void OutputDependencyFile();
172
173public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000174 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000175 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000176 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000177 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000178 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000179 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000180 IncludeModuleFiles(Opts.IncludeModuleFiles),
David Stenbergd45d7ea2018-05-29 13:07:58 +0000181 OutputFormat(Opts.OutputFormat),
182 InputFileIndex(0) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +0000183 for (const auto &ExtraDep : Opts.ExtraDeps) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000184 if (AddFilename(ExtraDep))
185 ++InputFileIndex;
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000186 }
187 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000188
Craig Topperafa7cb32014-03-13 06:07:04 +0000189 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
190 SrcMgr::CharacteristicKind FileType,
191 FileID PrevFID) override;
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000192
193 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
194 SrcMgr::CharacteristicKind FileType) override;
195
Craig Topperafa7cb32014-03-13 06:07:04 +0000196 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
197 StringRef FileName, bool IsAngled,
198 CharSourceRange FilenameRange, const FileEntry *File,
199 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +0000200 const Module *Imported,
201 SrcMgr::CharacteristicKind FileType) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000202
Craig Topperafa7cb32014-03-13 06:07:04 +0000203 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000204 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000205 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000206
David Stenbergd45d7ea2018-05-29 13:07:58 +0000207 bool AddFilename(StringRef Filename);
Ben Langmuircb69b572014-03-07 06:40:32 +0000208 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000209 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000210};
211
Richard Smith2a6edb32015-08-09 04:46:57 +0000212class DFGMMCallback : public ModuleMapCallbacks {
213 DFGImpl &Parent;
214public:
215 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
216 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
217 bool IsSystem) override {
218 if (!IsSystem || Parent.includeSystemHeaders())
219 Parent.AddFilename(Entry.getName());
220 }
221};
222
Ben Langmuircb69b572014-03-07 06:40:32 +0000223class DFGASTReaderListener : public ASTReaderListener {
224 DFGImpl &Parent;
225public:
226 DFGASTReaderListener(DFGImpl &Parent)
227 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000228 bool needsInputFileVisitation() override { return true; }
229 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000230 return Parent.includeSystemHeaders();
231 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000232 void visitModuleFile(StringRef Filename,
233 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000234 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000235 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000236};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000237}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000238
Ben Langmuircb69b572014-03-07 06:40:32 +0000239DependencyFileGenerator::DependencyFileGenerator(void *Impl)
240: Impl(Impl) { }
241
242DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
243 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
244
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000245 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000246 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000247 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000248 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000249
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000250 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000251 if (Opts.AddMissingHeaderDeps)
252 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000253
Ben Langmuircb69b572014-03-07 06:40:32 +0000254 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000255 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000256 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
257 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000258 return new DependencyFileGenerator(Callback);
259}
260
261void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
262 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
263 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000264 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000265}
266
267/// FileMatchesDepCriteria - Determine whether the given Filename should be
268/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000269bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
270 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000271 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000272 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000273
Eli Friedman33cd03c2009-05-19 03:35:57 +0000274 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000275 return true;
276
Richard Smithf3f84612017-06-29 02:19:42 +0000277 return !isSystem(FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000278}
279
Ben Langmuircb69b572014-03-07 06:40:32 +0000280void DFGImpl::FileChanged(SourceLocation Loc,
281 FileChangeReason Reason,
282 SrcMgr::CharacteristicKind FileType,
283 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000284 if (Reason != PPCallbacks::EnterFile)
285 return;
Mike Stump11289f42009-09-09 15:08:12 +0000286
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000287 // Dependency generation really does want to go all the way to the
288 // file entry for a source location to find out what is depended on.
289 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000290 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattner64d8fc22009-01-28 05:42:38 +0000292 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000293 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000294 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000296 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000297 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000298 return;
299
Douglas Katzman7f43af52015-09-02 21:14:53 +0000300 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000301}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000302
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000303void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
304 const Token &FilenameTok,
305 SrcMgr::CharacteristicKind FileType) {
306 StringRef Filename = SkippedFile.getName();
307 if (!FileMatchesDepCriteria(Filename.data(), FileType))
308 return;
309
310 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
311}
312
Ben Langmuircb69b572014-03-07 06:40:32 +0000313void DFGImpl::InclusionDirective(SourceLocation HashLoc,
314 const Token &IncludeTok,
315 StringRef FileName,
316 bool IsAngled,
317 CharSourceRange FilenameRange,
318 const FileEntry *File,
319 StringRef SearchPath,
320 StringRef RelativePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000321 const Module *Imported,
Julie Hockett96fbe582018-05-10 19:05:36 +0000322 SrcMgr::CharacteristicKind FileType) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000323 if (!File) {
324 if (AddMissingHeaderDeps)
325 AddFilename(FileName);
326 else
327 SeenMissingHeader = true;
328 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000329}
330
David Stenbergd45d7ea2018-05-29 13:07:58 +0000331bool DFGImpl::AddFilename(StringRef Filename) {
332 if (FilesSet.insert(Filename).second) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000333 Files.push_back(Filename);
David Stenbergd45d7ea2018-05-29 13:07:58 +0000334 return true;
335 }
336 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000337}
338
Paul Robinson64441de2015-05-13 21:18:15 +0000339/// Print the filename, with escaping or quoting that accommodates the three
340/// most likely tools that use dependency files: GNU Make, BSD Make, and
341/// NMake/Jom.
342///
343/// BSD Make is the simplest case: It does no escaping at all. This means
344/// characters that are normally delimiters, i.e. space and # (the comment
345/// character) simply aren't supported in filenames.
346///
347/// GNU Make does allow space and # in filenames, but to avoid being treated
348/// as a delimiter or comment, these must be escaped with a backslash. Because
349/// backslash is itself the escape character, if a backslash appears in a
350/// filename, it should be escaped as well. (As a special case, $ is escaped
351/// as $$, which is the normal Make way to handle the $ character.)
352/// For compatibility with BSD Make and historical practice, if GNU Make
353/// un-escapes characters in a filename but doesn't find a match, it will
354/// retry with the unmodified original string.
355///
356/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000357/// characters in the original filename, but not escaping backslashes. The
358/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000359/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
360/// unmodified original string; filenames with # or space characters aren't
361/// supported by BSD Make at all, but will be handled correctly by GNU Make
362/// due to the escaping.
363///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000364/// A corner case that GCC gets only partly right is when the original filename
365/// has a backslash immediately followed by space or #. GNU Make would expect
366/// this backslash to be escaped; however GCC escapes the original backslash
367/// only when followed by space, not #. It will therefore take a dependency
368/// from a directive such as
369/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000370/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000371/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000372/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000373/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000374/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000375/// original string, which probably doesn't exist either; in any case it won't
376/// find
377/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000378/// which is the actual filename specified by the include directive.
379///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000380/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000381///
382/// NMake/Jom has a different set of scary characters, but wraps filespecs in
383/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000384/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
385/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
386/// for Windows file-naming info.
387static void PrintFilename(raw_ostream &OS, StringRef Filename,
388 DependencyOutputFormat OutputFormat) {
David Bolvanskyc96cb252018-09-13 14:27:32 +0000389 // Convert filename to platform native path
390 llvm::SmallString<256> NativePath;
391 llvm::sys::path::native(Filename.str(), NativePath);
392
Paul Robinsond7214a72015-04-27 18:14:32 +0000393 if (OutputFormat == DependencyOutputFormat::NMake) {
394 // Add quotes if needed. These are the characters listed as "special" to
395 // NMake, that are legal in a Windows filespec, and that could cause
396 // misinterpretation of the dependency string.
David Bolvanskyc96cb252018-09-13 14:27:32 +0000397 if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
398 OS << '\"' << NativePath << '\"';
Paul Robinsond7214a72015-04-27 18:14:32 +0000399 else
David Bolvanskyc96cb252018-09-13 14:27:32 +0000400 OS << NativePath;
Paul Robinsond7214a72015-04-27 18:14:32 +0000401 return;
402 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000403 assert(OutputFormat == DependencyOutputFormat::Make);
David Bolvanskyc96cb252018-09-13 14:27:32 +0000404 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
405 if (NativePath[i] == '#') // Handle '#' the broken gcc way.
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000406 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000407 else if (NativePath[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000408 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000409 unsigned j = i;
David Bolvanskyc96cb252018-09-13 14:27:32 +0000410 while (j > 0 && NativePath[--j] == '\\')
Paul Robinson64441de2015-05-13 21:18:15 +0000411 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000412 } else if (NativePath[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000413 OS << '$';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000414 OS << NativePath[i];
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000415 }
416}
417
Ben Langmuircb69b572014-03-07 06:40:32 +0000418void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000419 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000420 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000421 return;
422 }
423
Rafael Espindoladae941a2014-08-25 18:17:04 +0000424 std::error_code EC;
425 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
426 if (EC) {
427 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
428 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000429 return;
430 }
431
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000432 // Write out the dependency targets, trying to avoid overly long
433 // lines when possible. We try our best to emit exactly the same
434 // dependency file as GCC (4.2), assuming the included files are the
435 // same.
436 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000437 unsigned Columns = 0;
438
Yaron Keren3998a092016-11-16 19:24:10 +0000439 for (StringRef Target : Targets) {
440 unsigned N = Target.size();
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000441 if (Columns == 0) {
442 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000443 } else if (Columns + N + 2 > MaxColumns) {
444 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000445 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000446 } else {
447 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000448 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000449 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000450 // Targets already quoted as needed.
Yaron Keren3998a092016-11-16 19:24:10 +0000451 OS << Target;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000452 }
453
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000454 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000455 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000457 // Now add each dependency in the order it was seen, but avoiding
458 // duplicates.
Yaron Keren3998a092016-11-16 19:24:10 +0000459 for (StringRef File : Files) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000460 // Start a new line if this would exceed the column limit. Make
461 // sure to leave space for a trailing " \" in case we need to
462 // break the line on the next iteration.
Yaron Keren3998a092016-11-16 19:24:10 +0000463 unsigned N = File.size();
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000464 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000465 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000466 Columns = 2;
467 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000468 OS << ' ';
Yaron Keren3998a092016-11-16 19:24:10 +0000469 PrintFilename(OS, File, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000470 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000471 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000472 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000473
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000474 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000475 if (PhonyTarget && !Files.empty()) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000476 unsigned Index = 0;
477 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
478 if (Index++ == InputFileIndex)
479 continue;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000480 OS << '\n';
Yaron Kerenfa367402017-01-14 21:12:08 +0000481 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000482 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000483 }
484 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000485}
486
Ben Langmuircb69b572014-03-07 06:40:32 +0000487bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000488 bool IsSystem, bool IsOverridden,
489 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000490 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000491 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000492 return true;
493
Ben Langmuircb69b572014-03-07 06:40:32 +0000494 Parent.AddFilename(Filename);
495 return true;
496}
497
Richard Smith216a3bd2015-08-13 17:57:10 +0000498void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
499 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000500 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000501 Parent.AddFilename(Filename);
502}