1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This code generates dependency files.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Frontend/Utils.h"
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Frontend/DependencyOutputOptions.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Lex/DirectoryLookup.h"
19 #include "clang/Lex/ModuleMap.h"
20 #include "clang/Lex/PPCallbacks.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Serialization/ASTReader.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
29 using namespace clang;
32 struct DepCollectorPPCallbacks : public PPCallbacks {
33 DependencyCollector &DepCollector;
35 DiagnosticsEngine &Diags;
36 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM,
37 DiagnosticsEngine &Diags)
38 : DepCollector(L), SM(SM), Diags(Diags) {}
40 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
41 SrcMgr::CharacteristicKind FileType,
42 FileID PrevFID) override {
43 if (Reason != PPCallbacks::EnterFile)
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 Optional<FileEntryRef> File =
50 SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc)));
55 llvm::sys::path::remove_leading_dotslash(File->getName());
57 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
59 /*IsModuleFile*/false, /*IsMissing*/false);
62 void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
63 SrcMgr::CharacteristicKind FileType) override {
65 llvm::sys::path::remove_leading_dotslash(SkippedFile.getName());
66 DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
67 /*IsSystem=*/isSystem(FileType),
68 /*IsModuleFile=*/false,
72 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
73 StringRef FileName, bool IsAngled,
74 CharSourceRange FilenameRange, const FileEntry *File,
75 StringRef SearchPath, StringRef RelativePath,
76 const Module *Imported,
77 SrcMgr::CharacteristicKind FileType) override {
79 DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
80 /*IsSystem*/false, /*IsModuleFile*/false,
82 // Files that actually exist are handled by FileChanged.
85 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
86 Optional<FileEntryRef> File,
87 SrcMgr::CharacteristicKind FileType) override {
91 llvm::sys::path::remove_leading_dotslash(File->getName());
92 DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
93 /*IsSystem=*/isSystem(FileType),
94 /*IsModuleFile=*/false,
98 void EndOfMainFile() override { DepCollector.finishedMainFile(Diags); }
101 struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
102 DependencyCollector &DepCollector;
103 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
105 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
106 bool IsSystem) override {
107 StringRef Filename = Entry.getName();
108 DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
109 /*IsSystem*/IsSystem,
110 /*IsModuleFile*/false,
115 struct DepCollectorASTListener : public ASTReaderListener {
116 DependencyCollector &DepCollector;
117 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
118 bool needsInputFileVisitation() override { return true; }
119 bool needsSystemInputFileVisitation() override {
120 return DepCollector.needSystemDependencies();
122 void visitModuleFile(StringRef Filename,
123 serialization::ModuleKind Kind) override {
124 DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
125 /*IsSystem*/false, /*IsModuleFile*/true,
128 bool visitInputFile(StringRef Filename, bool IsSystem,
129 bool IsOverridden, bool IsExplicitModule) override {
130 if (IsOverridden || IsExplicitModule)
133 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
134 /*IsModuleFile*/false, /*IsMissing*/false);
138 } // end anonymous namespace
140 void DependencyCollector::maybeAddDependency(StringRef Filename,
141 bool FromModule, bool IsSystem,
144 if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
145 addDependency(Filename);
148 bool DependencyCollector::addDependency(StringRef Filename) {
149 if (Seen.insert(Filename).second) {
150 Dependencies.push_back(std::string(Filename));
156 static bool isSpecialFilename(StringRef Filename) {
157 return llvm::StringSwitch<bool>(Filename)
158 .Case("<built-in>", true)
159 .Case("<stdin>", true)
163 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
164 bool IsSystem, bool IsModuleFile,
166 return !isSpecialFilename(Filename) &&
167 (needSystemDependencies() || !IsSystem);
170 DependencyCollector::~DependencyCollector() { }
171 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
172 PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(
173 *this, PP.getSourceManager(), PP.getDiagnostics()));
174 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
175 std::make_unique<DepCollectorMMCallbacks>(*this));
177 void DependencyCollector::attachToASTReader(ASTReader &R) {
178 R.addListener(std::make_unique<DepCollectorASTListener>(*this));
181 DependencyFileGenerator::DependencyFileGenerator(
182 const DependencyOutputOptions &Opts)
183 : OutputFile(Opts.OutputFile), Targets(Opts.Targets),
184 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
185 PhonyTarget(Opts.UsePhonyTargets),
186 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),
187 IncludeModuleFiles(Opts.IncludeModuleFiles),
188 OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
189 for (const auto &ExtraDep : Opts.ExtraDeps) {
190 if (addDependency(ExtraDep))
195 void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
196 // Disable the "file not found" diagnostic if the -MG option was given.
197 if (AddMissingHeaderDeps)
198 PP.SetSuppressIncludeNotFoundError(true);
200 DependencyCollector::attachToPreprocessor(PP);
203 bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
204 bool IsSystem, bool IsModuleFile,
207 // Handle the case of missing file from an inclusion directive.
208 if (AddMissingHeaderDeps)
210 SeenMissingHeader = true;
213 if (IsModuleFile && !IncludeModuleFiles)
216 if (isSpecialFilename(Filename))
219 if (IncludeSystemHeaders)
225 void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
226 outputDependencyFile(Diags);
229 /// Print the filename, with escaping or quoting that accommodates the three
230 /// most likely tools that use dependency files: GNU Make, BSD Make, and
233 /// BSD Make is the simplest case: It does no escaping at all. This means
234 /// characters that are normally delimiters, i.e. space and # (the comment
235 /// character) simply aren't supported in filenames.
237 /// GNU Make does allow space and # in filenames, but to avoid being treated
238 /// as a delimiter or comment, these must be escaped with a backslash. Because
239 /// backslash is itself the escape character, if a backslash appears in a
240 /// filename, it should be escaped as well. (As a special case, $ is escaped
241 /// as $$, which is the normal Make way to handle the $ character.)
242 /// For compatibility with BSD Make and historical practice, if GNU Make
243 /// un-escapes characters in a filename but doesn't find a match, it will
244 /// retry with the unmodified original string.
246 /// GCC tries to accommodate both Make formats by escaping any space or #
247 /// characters in the original filename, but not escaping backslashes. The
248 /// apparent intent is so that filenames with backslashes will be handled
249 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
250 /// unmodified original string; filenames with # or space characters aren't
251 /// supported by BSD Make at all, but will be handled correctly by GNU Make
252 /// due to the escaping.
254 /// A corner case that GCC gets only partly right is when the original filename
255 /// has a backslash immediately followed by space or #. GNU Make would expect
256 /// this backslash to be escaped; however GCC escapes the original backslash
257 /// only when followed by space, not #. It will therefore take a dependency
258 /// from a directive such as
259 /// #include "a\ b\#c.h"
262 /// which GNU Make will interpret as
264 /// followed by a comment. Failing to find this file, it will fall back to the
265 /// original string, which probably doesn't exist either; in any case it won't
268 /// which is the actual filename specified by the include directive.
270 /// Clang does what GCC does, rather than what GNU Make expects.
272 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
273 /// double-quotes to avoid misinterpreting them; see
274 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
275 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
276 /// for Windows file-naming info.
277 static void PrintFilename(raw_ostream &OS, StringRef Filename,
278 DependencyOutputFormat OutputFormat) {
279 // Convert filename to platform native path
280 llvm::SmallString<256> NativePath;
281 llvm::sys::path::native(Filename.str(), NativePath);
283 if (OutputFormat == DependencyOutputFormat::NMake) {
284 // Add quotes if needed. These are the characters listed as "special" to
285 // NMake, that are legal in a Windows filespec, and that could cause
286 // misinterpretation of the dependency string.
287 if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
288 OS << '\"' << NativePath << '\"';
293 assert(OutputFormat == DependencyOutputFormat::Make);
294 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
295 if (NativePath[i] == '#') // Handle '#' the broken gcc way.
297 else if (NativePath[i] == ' ') { // Handle space correctly.
300 while (j > 0 && NativePath[--j] == '\\')
302 } else if (NativePath[i] == '$') // $ is escaped by $$.
308 void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
309 if (SeenMissingHeader) {
310 llvm::sys::fs::remove(OutputFile);
315 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
317 Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();
321 outputDependencyFile(OS);
324 void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
325 // Write out the dependency targets, trying to avoid overly long
326 // lines when possible. We try our best to emit exactly the same
327 // dependency file as GCC (4.2), assuming the included files are the
329 const unsigned MaxColumns = 75;
330 unsigned Columns = 0;
332 for (StringRef Target : Targets) {
333 unsigned N = Target.size();
336 } else if (Columns + N + 2 > MaxColumns) {
343 // Targets already quoted as needed.
350 // Now add each dependency in the order it was seen, but avoiding
352 ArrayRef<std::string> Files = getDependencies();
353 for (StringRef File : Files) {
354 // Start a new line if this would exceed the column limit. Make
355 // sure to leave space for a trailing " \" in case we need to
356 // break the line on the next iteration.
357 unsigned N = File.size();
358 if (Columns + (N + 1) + 2 > MaxColumns) {
363 PrintFilename(OS, File, OutputFormat);
368 // Create phony targets if requested.
369 if (PhonyTarget && !Files.empty()) {
371 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
372 if (Index++ == InputFileIndex)
375 PrintFilename(OS, *I, OutputFormat);