blob: a03d4b79c8b94ec1e5277b3b0e3f14f7a7b7a115 [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"
Richard Smith2a6edb32015-08-09 04:46:57 +000020#include "clang/Lex/ModuleMap.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"
David Majnemerf0822fb2014-10-27 22:31:50 +000025#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer33d43302013-06-13 14:26:04 +000026#include "llvm/Support/FileSystem.h"
Eli Friedmanf7ca26a2011-07-08 20:17:28 +000027#include "llvm/Support/Path.h"
Daniel Dunbar966c2e12008-10-27 20:01:06 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbarfa0caca2008-10-24 22:12:41 +000029
30using namespace clang;
31
32namespace {
Ben Langmuir33c80902014-06-30 20:04:14 +000033struct DepCollectorPPCallbacks : public PPCallbacks {
34 DependencyCollector &DepCollector;
35 SourceManager &SM;
36 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM)
37 : DepCollector(L), SM(SM) { }
38
39 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
40 SrcMgr::CharacteristicKind FileType,
41 FileID PrevFID) override {
42 if (Reason != PPCallbacks::EnterFile)
43 return;
44
45 // Dependency generation really does want to go all the way to the
46 // file entry for a source location to find out what is depended on.
47 // We do not want #line markers to affect dependency generation!
48 const FileEntry *FE =
49 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
50 if (!FE)
51 return;
52
Douglas Katzman7f43af52015-09-02 21:14:53 +000053 StringRef Filename =
54 llvm::sys::path::remove_leading_dotslash(FE->getName());
Ben Langmuir33c80902014-06-30 20:04:14 +000055
56 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
Richard Smithf3f84612017-06-29 02:19:42 +000057 isSystem(FileType),
58 /*IsModuleFile*/false, /*IsMissing*/false);
Ben Langmuir33c80902014-06-30 20:04:14 +000059 }
60
61 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
62 StringRef FileName, bool IsAngled,
63 CharSourceRange FilenameRange, const FileEntry *File,
64 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +000065 const Module *Imported,
66 SrcMgr::CharacteristicKind FileType) override {
Ben Langmuir33c80902014-06-30 20:04:14 +000067 if (!File)
68 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
69 /*IsSystem*/false, /*IsModuleFile*/false,
70 /*IsMissing*/true);
71 // Files that actually exist are handled by FileChanged.
72 }
73
74 void EndOfMainFile() override {
75 DepCollector.finishedMainFile();
76 }
77};
78
Richard Smith2a6edb32015-08-09 04:46:57 +000079struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
80 DependencyCollector &DepCollector;
81 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
82
83 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
84 bool IsSystem) override {
85 StringRef Filename = Entry.getName();
86 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
87 /*IsSystem*/IsSystem,
88 /*IsModuleFile*/false,
89 /*IsMissing*/false);
90 }
91};
92
Ben Langmuir33c80902014-06-30 20:04:14 +000093struct DepCollectorASTListener : public ASTReaderListener {
94 DependencyCollector &DepCollector;
95 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
96 bool needsInputFileVisitation() override { return true; }
97 bool needsSystemInputFileVisitation() override {
98 return DepCollector.needSystemDependencies();
99 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000100 void visitModuleFile(StringRef Filename,
101 serialization::ModuleKind Kind) override {
Ben Langmuir33c80902014-06-30 20:04:14 +0000102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
103 /*IsSystem*/false, /*IsModuleFile*/true,
104 /*IsMissing*/false);
105 }
106 bool visitInputFile(StringRef Filename, bool IsSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000107 bool IsOverridden, bool IsExplicitModule) override {
108 if (IsOverridden || IsExplicitModule)
Ben Langmuir33c80902014-06-30 20:04:14 +0000109 return true;
110
111 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
112 /*IsModuleFile*/false, /*IsMissing*/false);
113 return true;
114 }
115};
116} // end anonymous namespace
117
118void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule,
119 bool IsSystem, bool IsModuleFile,
120 bool IsMissing) {
David Blaikie61b86d42014-11-19 02:56:13 +0000121 if (Seen.insert(Filename).second &&
Ben Langmuir33c80902014-06-30 20:04:14 +0000122 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
123 Dependencies.push_back(Filename);
124}
125
David Majnemerf0822fb2014-10-27 22:31:50 +0000126static bool isSpecialFilename(StringRef Filename) {
127 return llvm::StringSwitch<bool>(Filename)
128 .Case("<built-in>", true)
129 .Case("<stdin>", true)
130 .Default(false);
131}
132
Ben Langmuir33c80902014-06-30 20:04:14 +0000133bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
134 bool IsSystem, bool IsModuleFile,
135 bool IsMissing) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000136 return !isSpecialFilename(Filename) &&
137 (needSystemDependencies() || !IsSystem);
Ben Langmuir33c80902014-06-30 20:04:14 +0000138}
139
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000140DependencyCollector::~DependencyCollector() { }
Ben Langmuir33c80902014-06-30 20:04:14 +0000141void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
Craig Topperb8a70532014-09-10 04:53:53 +0000142 PP.addPPCallbacks(
143 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager()));
Richard Smith2a6edb32015-08-09 04:46:57 +0000144 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
145 llvm::make_unique<DepCollectorMMCallbacks>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000146}
147void DependencyCollector::attachToASTReader(ASTReader &R) {
David Blaikie2721c322014-08-10 16:54:39 +0000148 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this));
Ben Langmuir33c80902014-06-30 20:04:14 +0000149}
150
151namespace {
Ben Langmuircb69b572014-03-07 06:40:32 +0000152/// Private implementation for DependencyFileGenerator
153class DFGImpl : public PPCallbacks {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000154 std::vector<std::string> Files;
155 llvm::StringSet<> FilesSet;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000156 const Preprocessor *PP;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000157 std::string OutputFile;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000158 std::vector<std::string> Targets;
Eli Friedman33cd03c2009-05-19 03:35:57 +0000159 bool IncludeSystemHeaders;
160 bool PhonyTarget;
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000161 bool AddMissingHeaderDeps;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000162 bool SeenMissingHeader;
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000163 bool IncludeModuleFiles;
Paul Robinsond7214a72015-04-27 18:14:32 +0000164 DependencyOutputFormat OutputFormat;
David Stenbergd45d7ea2018-05-29 13:07:58 +0000165 unsigned InputFileIndex;
Paul Robinsond7214a72015-04-27 18:14:32 +0000166
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000167private:
168 bool FileMatchesDepCriteria(const char *Filename,
Chris Lattner66a740e2008-10-27 01:19:25 +0000169 SrcMgr::CharacteristicKind FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000170 void OutputDependencyFile();
171
172public:
Ben Langmuircb69b572014-03-07 06:40:32 +0000173 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts)
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000174 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000175 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000176 PhonyTarget(Opts.UsePhonyTargets),
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000177 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000178 SeenMissingHeader(false),
Paul Robinsond7214a72015-04-27 18:14:32 +0000179 IncludeModuleFiles(Opts.IncludeModuleFiles),
David Stenbergd45d7ea2018-05-29 13:07:58 +0000180 OutputFormat(Opts.OutputFormat),
181 InputFileIndex(0) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +0000182 for (const auto &ExtraDep : Opts.ExtraDeps) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000183 if (AddFilename(ExtraDep))
184 ++InputFileIndex;
Ivan Krasin1193f2c2015-08-13 04:04:37 +0000185 }
186 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000187
Craig Topperafa7cb32014-03-13 06:07:04 +0000188 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
189 SrcMgr::CharacteristicKind FileType,
190 FileID PrevFID) override;
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000191
192 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
193 SrcMgr::CharacteristicKind FileType) override;
194
Craig Topperafa7cb32014-03-13 06:07:04 +0000195 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
196 StringRef FileName, bool IsAngled,
197 CharSourceRange FilenameRange, const FileEntry *File,
198 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +0000199 const Module *Imported,
200 SrcMgr::CharacteristicKind FileType) override;
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000201
Volodymyr Sapsaia4c53282018-09-18 23:27:02 +0000202 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
203 const FileEntry *File,
204 SrcMgr::CharacteristicKind FileType) override;
205
Craig Topperafa7cb32014-03-13 06:07:04 +0000206 void EndOfMainFile() override {
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000207 OutputDependencyFile();
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000208 }
Ben Langmuircb69b572014-03-07 06:40:32 +0000209
David Stenbergd45d7ea2018-05-29 13:07:58 +0000210 bool AddFilename(StringRef Filename);
Ben Langmuircb69b572014-03-07 06:40:32 +0000211 bool includeSystemHeaders() const { return IncludeSystemHeaders; }
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000212 bool includeModuleFiles() const { return IncludeModuleFiles; }
Ben Langmuircb69b572014-03-07 06:40:32 +0000213};
214
Richard Smith2a6edb32015-08-09 04:46:57 +0000215class DFGMMCallback : public ModuleMapCallbacks {
216 DFGImpl &Parent;
217public:
218 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {}
219 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
220 bool IsSystem) override {
221 if (!IsSystem || Parent.includeSystemHeaders())
222 Parent.AddFilename(Entry.getName());
223 }
224};
225
Ben Langmuircb69b572014-03-07 06:40:32 +0000226class DFGASTReaderListener : public ASTReaderListener {
227 DFGImpl &Parent;
228public:
229 DFGASTReaderListener(DFGImpl &Parent)
230 : Parent(Parent) { }
Craig Topperafa7cb32014-03-13 06:07:04 +0000231 bool needsInputFileVisitation() override { return true; }
232 bool needsSystemInputFileVisitation() override {
Ben Langmuircb69b572014-03-07 06:40:32 +0000233 return Parent.includeSystemHeaders();
234 }
Richard Smith216a3bd2015-08-13 17:57:10 +0000235 void visitModuleFile(StringRef Filename,
236 serialization::ModuleKind Kind) override;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000237 bool visitInputFile(StringRef Filename, bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000238 bool isOverridden, bool isExplicitModule) override;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000239};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000240}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000241
Ben Langmuircb69b572014-03-07 06:40:32 +0000242DependencyFileGenerator::DependencyFileGenerator(void *Impl)
243: Impl(Impl) { }
244
245DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor(
246 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) {
247
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000248 if (Opts.Targets.empty()) {
Daniel Dunbar55b781f2009-11-11 21:44:00 +0000249 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
Craig Topper49a27902014-05-22 04:46:25 +0000250 return nullptr;
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000251 }
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000252
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000253 // Disable the "file not found" diagnostic if the -MG option was given.
Eli Friedman3781a362011-08-30 23:07:51 +0000254 if (Opts.AddMissingHeaderDeps)
255 PP.SetSuppressIncludeNotFoundError(true);
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000256
Ben Langmuircb69b572014-03-07 06:40:32 +0000257 DFGImpl *Callback = new DFGImpl(&PP, Opts);
Craig Topperb8a70532014-09-10 04:53:53 +0000258 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback));
Richard Smith2a6edb32015-08-09 04:46:57 +0000259 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
260 llvm::make_unique<DFGMMCallback>(*Callback));
Ben Langmuircb69b572014-03-07 06:40:32 +0000261 return new DependencyFileGenerator(Callback);
262}
263
264void DependencyFileGenerator::AttachToASTReader(ASTReader &R) {
265 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl);
266 assert(I && "missing implementation");
David Blaikie2721c322014-08-10 16:54:39 +0000267 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I));
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000268}
269
270/// FileMatchesDepCriteria - Determine whether the given Filename should be
271/// considered as a dependency.
Ben Langmuircb69b572014-03-07 06:40:32 +0000272bool DFGImpl::FileMatchesDepCriteria(const char *Filename,
273 SrcMgr::CharacteristicKind FileType) {
David Majnemerf0822fb2014-10-27 22:31:50 +0000274 if (isSpecialFilename(Filename))
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000275 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000276
Eli Friedman33cd03c2009-05-19 03:35:57 +0000277 if (IncludeSystemHeaders)
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000278 return true;
279
Richard Smithf3f84612017-06-29 02:19:42 +0000280 return !isSystem(FileType);
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000281}
282
Ben Langmuircb69b572014-03-07 06:40:32 +0000283void DFGImpl::FileChanged(SourceLocation Loc,
284 FileChangeReason Reason,
285 SrcMgr::CharacteristicKind FileType,
286 FileID PrevFID) {
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000287 if (Reason != PPCallbacks::EnterFile)
288 return;
Mike Stump11289f42009-09-09 15:08:12 +0000289
Daniel Dunbar52e96cc2009-03-30 00:34:04 +0000290 // Dependency generation really does want to go all the way to the
291 // file entry for a source location to find out what is depended on.
292 // We do not want #line markers to affect dependency generation!
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000293 SourceManager &SM = PP->getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattner64d8fc22009-01-28 05:42:38 +0000295 const FileEntry *FE =
Chandler Carruth35f53202011-07-25 16:49:02 +0000296 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
Craig Topper49a27902014-05-22 04:46:25 +0000297 if (!FE) return;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000299 StringRef Filename = FE->getName();
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000300 if (!FileMatchesDepCriteria(Filename.data(), FileType))
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000301 return;
302
Douglas Katzman7f43af52015-09-02 21:14:53 +0000303 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000304}
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000305
Volodymyr Sapsai210f0e82018-05-01 23:59:33 +0000306void DFGImpl::FileSkipped(const FileEntry &SkippedFile,
307 const Token &FilenameTok,
308 SrcMgr::CharacteristicKind FileType) {
309 StringRef Filename = SkippedFile.getName();
310 if (!FileMatchesDepCriteria(Filename.data(), FileType))
311 return;
312
313 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
314}
315
Ben Langmuircb69b572014-03-07 06:40:32 +0000316void DFGImpl::InclusionDirective(SourceLocation HashLoc,
317 const Token &IncludeTok,
318 StringRef FileName,
319 bool IsAngled,
320 CharSourceRange FilenameRange,
321 const FileEntry *File,
322 StringRef SearchPath,
323 StringRef RelativePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000324 const Module *Imported,
Julie Hockett96fbe582018-05-10 19:05:36 +0000325 SrcMgr::CharacteristicKind FileType) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000326 if (!File) {
327 if (AddMissingHeaderDeps)
328 AddFilename(FileName);
329 else
330 SeenMissingHeader = true;
331 }
Peter Collingbourne77b0e7f22011-07-12 19:35:15 +0000332}
333
Volodymyr Sapsaia4c53282018-09-18 23:27:02 +0000334void DFGImpl::HasInclude(SourceLocation Loc, StringRef SpelledFilename,
335 bool IsAngled, const FileEntry *File,
336 SrcMgr::CharacteristicKind FileType) {
337 if (!File)
338 return;
339 StringRef Filename = File->getName();
340 if (!FileMatchesDepCriteria(Filename.data(), FileType))
341 return;
342 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename));
343}
344
David Stenbergd45d7ea2018-05-29 13:07:58 +0000345bool DFGImpl::AddFilename(StringRef Filename) {
346 if (FilesSet.insert(Filename).second) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000347 Files.push_back(Filename);
David Stenbergd45d7ea2018-05-29 13:07:58 +0000348 return true;
349 }
350 return false;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000351}
352
Paul Robinson64441de2015-05-13 21:18:15 +0000353/// Print the filename, with escaping or quoting that accommodates the three
354/// most likely tools that use dependency files: GNU Make, BSD Make, and
355/// NMake/Jom.
356///
357/// BSD Make is the simplest case: It does no escaping at all. This means
358/// characters that are normally delimiters, i.e. space and # (the comment
359/// character) simply aren't supported in filenames.
360///
361/// GNU Make does allow space and # in filenames, but to avoid being treated
362/// as a delimiter or comment, these must be escaped with a backslash. Because
363/// backslash is itself the escape character, if a backslash appears in a
364/// filename, it should be escaped as well. (As a special case, $ is escaped
365/// as $$, which is the normal Make way to handle the $ character.)
366/// For compatibility with BSD Make and historical practice, if GNU Make
367/// un-escapes characters in a filename but doesn't find a match, it will
368/// retry with the unmodified original string.
369///
370/// GCC tries to accommodate both Make formats by escaping any space or #
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000371/// characters in the original filename, but not escaping backslashes. The
372/// apparent intent is so that filenames with backslashes will be handled
Paul Robinson64441de2015-05-13 21:18:15 +0000373/// correctly by BSD Make, and by GNU Make in its fallback mode of using the
374/// unmodified original string; filenames with # or space characters aren't
375/// supported by BSD Make at all, but will be handled correctly by GNU Make
376/// due to the escaping.
377///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000378/// A corner case that GCC gets only partly right is when the original filename
379/// has a backslash immediately followed by space or #. GNU Make would expect
380/// this backslash to be escaped; however GCC escapes the original backslash
381/// only when followed by space, not #. It will therefore take a dependency
382/// from a directive such as
383/// #include "a\ b\#c.h"
Paul Robinson64441de2015-05-13 21:18:15 +0000384/// and emit it as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000385/// a\\\ b\\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000386/// which GNU Make will interpret as
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000387/// a\ b\
Paul Robinson64441de2015-05-13 21:18:15 +0000388/// followed by a comment. Failing to find this file, it will fall back to the
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000389/// original string, which probably doesn't exist either; in any case it won't
390/// find
391/// a\ b\#c.h
Paul Robinson64441de2015-05-13 21:18:15 +0000392/// which is the actual filename specified by the include directive.
393///
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000394/// Clang does what GCC does, rather than what GNU Make expects.
Paul Robinson64441de2015-05-13 21:18:15 +0000395///
396/// NMake/Jom has a different set of scary characters, but wraps filespecs in
397/// double-quotes to avoid misinterpreting them; see
Paul Robinsond7214a72015-04-27 18:14:32 +0000398/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
399/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
400/// for Windows file-naming info.
401static void PrintFilename(raw_ostream &OS, StringRef Filename,
402 DependencyOutputFormat OutputFormat) {
David Bolvanskyc96cb252018-09-13 14:27:32 +0000403 // Convert filename to platform native path
404 llvm::SmallString<256> NativePath;
405 llvm::sys::path::native(Filename.str(), NativePath);
406
Paul Robinsond7214a72015-04-27 18:14:32 +0000407 if (OutputFormat == DependencyOutputFormat::NMake) {
408 // Add quotes if needed. These are the characters listed as "special" to
409 // NMake, that are legal in a Windows filespec, and that could cause
410 // misinterpretation of the dependency string.
David Bolvanskyc96cb252018-09-13 14:27:32 +0000411 if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
412 OS << '\"' << NativePath << '\"';
Paul Robinsond7214a72015-04-27 18:14:32 +0000413 else
David Bolvanskyc96cb252018-09-13 14:27:32 +0000414 OS << NativePath;
Paul Robinsond7214a72015-04-27 18:14:32 +0000415 return;
416 }
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000417 assert(OutputFormat == DependencyOutputFormat::Make);
David Bolvanskyc96cb252018-09-13 14:27:32 +0000418 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
419 if (NativePath[i] == '#') // Handle '#' the broken gcc way.
Paul Robinsonfcdf3e92015-05-13 22:33:50 +0000420 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000421 else if (NativePath[i] == ' ') { // Handle space correctly.
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000422 OS << '\\';
Paul Robinson64441de2015-05-13 21:18:15 +0000423 unsigned j = i;
David Bolvanskyc96cb252018-09-13 14:27:32 +0000424 while (j > 0 && NativePath[--j] == '\\')
Paul Robinson64441de2015-05-13 21:18:15 +0000425 OS << '\\';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000426 } else if (NativePath[i] == '$') // $ is escaped by $$.
Benjamin Kramerae611512013-04-02 13:38:48 +0000427 OS << '$';
David Bolvanskyc96cb252018-09-13 14:27:32 +0000428 OS << NativePath[i];
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000429 }
430}
431
Ben Langmuircb69b572014-03-07 06:40:32 +0000432void DFGImpl::OutputDependencyFile() {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000433 if (SeenMissingHeader) {
Rafael Espindola2a008782014-01-10 21:32:14 +0000434 llvm::sys::fs::remove(OutputFile);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000435 return;
436 }
437
Rafael Espindoladae941a2014-08-25 18:17:04 +0000438 std::error_code EC;
439 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
440 if (EC) {
441 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile
442 << EC.message();
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000443 return;
444 }
445
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000446 // Write out the dependency targets, trying to avoid overly long
447 // lines when possible. We try our best to emit exactly the same
448 // dependency file as GCC (4.2), assuming the included files are the
449 // same.
450 const unsigned MaxColumns = 75;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000451 unsigned Columns = 0;
452
Yaron Keren3998a092016-11-16 19:24:10 +0000453 for (StringRef Target : Targets) {
454 unsigned N = Target.size();
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000455 if (Columns == 0) {
456 Columns += N;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000457 } else if (Columns + N + 2 > MaxColumns) {
458 Columns = N + 2;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000459 OS << " \\\n ";
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000460 } else {
461 Columns += N + 1;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000462 OS << ' ';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000463 }
Chris Lattner5df2a4e2011-02-17 02:14:49 +0000464 // Targets already quoted as needed.
Yaron Keren3998a092016-11-16 19:24:10 +0000465 OS << Target;
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000466 }
467
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000468 OS << ':';
Chris Lattnerfa5880e2009-01-11 19:28:34 +0000469 Columns += 1;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000471 // Now add each dependency in the order it was seen, but avoiding
472 // duplicates.
Yaron Keren3998a092016-11-16 19:24:10 +0000473 for (StringRef File : Files) {
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000474 // Start a new line if this would exceed the column limit. Make
475 // sure to leave space for a trailing " \" in case we need to
476 // break the line on the next iteration.
Yaron Keren3998a092016-11-16 19:24:10 +0000477 unsigned N = File.size();
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000478 if (Columns + (N + 1) + 2 > MaxColumns) {
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000479 OS << " \\\n ";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000480 Columns = 2;
481 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000482 OS << ' ';
Yaron Keren3998a092016-11-16 19:24:10 +0000483 PrintFilename(OS, File, OutputFormat);
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000484 Columns += N + 1;
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000485 }
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000486 OS << '\n';
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000487
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000488 // Create phony targets if requested.
Fariborz Jahanian193f7832011-04-15 18:49:23 +0000489 if (PhonyTarget && !Files.empty()) {
David Stenbergd45d7ea2018-05-29 13:07:58 +0000490 unsigned Index = 0;
491 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
492 if (Index++ == InputFileIndex)
493 continue;
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000494 OS << '\n';
Yaron Kerenfa367402017-01-14 21:12:08 +0000495 PrintFilename(OS, *I, OutputFormat);
Peter Collingbourne0e7e3fc2011-11-21 00:01:14 +0000496 OS << ":\n";
Daniel Dunbar966c2e12008-10-27 20:01:06 +0000497 }
498 }
Daniel Dunbarfa0caca2008-10-24 22:12:41 +0000499}
500
Ben Langmuircb69b572014-03-07 06:40:32 +0000501bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename,
Richard Smith216a3bd2015-08-13 17:57:10 +0000502 bool IsSystem, bool IsOverridden,
503 bool IsExplicitModule) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000504 assert(!IsSystem || needsSystemInputFileVisitation());
Richard Smith216a3bd2015-08-13 17:57:10 +0000505 if (IsOverridden || IsExplicitModule)
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000506 return true;
507
Ben Langmuircb69b572014-03-07 06:40:32 +0000508 Parent.AddFilename(Filename);
509 return true;
510}
511
Richard Smith216a3bd2015-08-13 17:57:10 +0000512void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename,
513 serialization::ModuleKind Kind) {
Richard Smithbe9b6c72015-08-13 18:30:25 +0000514 if (Parent.includeModuleFiles())
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000515 Parent.AddFilename(Filename);
516}