xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/DependencyFile.cpp (revision 43e29d03f416d7dda52112a29600a7c82ee1a91e)
1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This code generates dependency files.
10 //
11 //===----------------------------------------------------------------------===//
12 
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/Support/FileSystem.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <optional>
28 
29 using namespace clang;
30 
31 namespace {
32 struct DepCollectorPPCallbacks : public PPCallbacks {
33   DependencyCollector &DepCollector;
34   Preprocessor &PP;
35   DepCollectorPPCallbacks(DependencyCollector &L, Preprocessor &PP)
36       : DepCollector(L), PP(PP) {}
37 
38   void LexedFileChanged(FileID FID, LexedFileChangeReason Reason,
39                         SrcMgr::CharacteristicKind FileType, FileID PrevFID,
40                         SourceLocation Loc) override {
41     if (Reason != PPCallbacks::LexedFileChangeReason::EnterFile)
42       return;
43 
44     // Dependency generation really does want to go all the way to the
45     // file entry for a source location to find out what is depended on.
46     // We do not want #line markers to affect dependency generation!
47     if (std::optional<StringRef> Filename =
48             PP.getSourceManager().getNonBuiltinFilenameForID(FID))
49       DepCollector.maybeAddDependency(
50           llvm::sys::path::remove_leading_dotslash(*Filename),
51           /*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false,
52           /*IsMissing*/ false);
53   }
54 
55   void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
56                    SrcMgr::CharacteristicKind FileType) override {
57     StringRef Filename =
58         llvm::sys::path::remove_leading_dotslash(SkippedFile.getName());
59     DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
60                                     /*IsSystem=*/isSystem(FileType),
61                                     /*IsModuleFile=*/false,
62                                     /*IsMissing=*/false);
63   }
64 
65   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
66                           StringRef FileName, bool IsAngled,
67                           CharSourceRange FilenameRange,
68                           OptionalFileEntryRef File, StringRef SearchPath,
69                           StringRef RelativePath, const Module *Imported,
70                           SrcMgr::CharacteristicKind FileType) override {
71     if (!File)
72       DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
73                                      /*IsSystem*/false, /*IsModuleFile*/false,
74                                      /*IsMissing*/true);
75     // Files that actually exist are handled by FileChanged.
76   }
77 
78   void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
79                   OptionalFileEntryRef File,
80                   SrcMgr::CharacteristicKind FileType) override {
81     if (!File)
82       return;
83     StringRef Filename =
84         llvm::sys::path::remove_leading_dotslash(File->getName());
85     DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,
86                                     /*IsSystem=*/isSystem(FileType),
87                                     /*IsModuleFile=*/false,
88                                     /*IsMissing=*/false);
89   }
90 
91   void EndOfMainFile() override {
92     DepCollector.finishedMainFile(PP.getDiagnostics());
93   }
94 };
95 
96 struct DepCollectorMMCallbacks : public ModuleMapCallbacks {
97   DependencyCollector &DepCollector;
98   DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}
99 
100   void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry,
101                          bool IsSystem) override {
102     StringRef Filename = Entry.getName();
103     DepCollector.maybeAddDependency(Filename, /*FromModule*/false,
104                                     /*IsSystem*/IsSystem,
105                                     /*IsModuleFile*/false,
106                                     /*IsMissing*/false);
107   }
108 };
109 
110 struct DepCollectorASTListener : public ASTReaderListener {
111   DependencyCollector &DepCollector;
112   FileManager &FileMgr;
113   DepCollectorASTListener(DependencyCollector &L, FileManager &FileMgr)
114       : DepCollector(L), FileMgr(FileMgr) {}
115   bool needsInputFileVisitation() override { return true; }
116   bool needsSystemInputFileVisitation() override {
117     return DepCollector.needSystemDependencies();
118   }
119   void visitModuleFile(StringRef Filename,
120                        serialization::ModuleKind Kind) override {
121     DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
122                                    /*IsSystem*/false, /*IsModuleFile*/true,
123                                    /*IsMissing*/false);
124   }
125   bool visitInputFile(StringRef Filename, bool IsSystem,
126                       bool IsOverridden, bool IsExplicitModule) override {
127     if (IsOverridden || IsExplicitModule)
128       return true;
129 
130     // Run this through the FileManager in order to respect 'use-external-name'
131     // in case we have a VFS overlay.
132     if (auto FE = FileMgr.getOptionalFileRef(Filename))
133       Filename = FE->getName();
134 
135     DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
136                                    /*IsModuleFile*/false, /*IsMissing*/false);
137     return true;
138   }
139 };
140 } // end anonymous namespace
141 
142 void DependencyCollector::maybeAddDependency(StringRef Filename,
143                                              bool FromModule, bool IsSystem,
144                                              bool IsModuleFile,
145                                              bool IsMissing) {
146   if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
147     addDependency(Filename);
148 }
149 
150 bool DependencyCollector::addDependency(StringRef Filename) {
151   StringRef SearchPath;
152 #ifdef _WIN32
153   // Make the search insensitive to case and separators.
154   llvm::SmallString<256> TmpPath = Filename;
155   llvm::sys::path::native(TmpPath);
156   std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower);
157   SearchPath = TmpPath.str();
158 #else
159   SearchPath = Filename;
160 #endif
161 
162   if (Seen.insert(SearchPath).second) {
163     Dependencies.push_back(std::string(Filename));
164     return true;
165   }
166   return false;
167 }
168 
169 static bool isSpecialFilename(StringRef Filename) {
170   return Filename == "<built-in>";
171 }
172 
173 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
174                                         bool IsSystem, bool IsModuleFile,
175                                         bool IsMissing) {
176   return !isSpecialFilename(Filename) &&
177          (needSystemDependencies() || !IsSystem);
178 }
179 
180 DependencyCollector::~DependencyCollector() { }
181 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
182   PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*this, PP));
183   PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
184       std::make_unique<DepCollectorMMCallbacks>(*this));
185 }
186 void DependencyCollector::attachToASTReader(ASTReader &R) {
187   R.addListener(
188       std::make_unique<DepCollectorASTListener>(*this, R.getFileManager()));
189 }
190 
191 DependencyFileGenerator::DependencyFileGenerator(
192     const DependencyOutputOptions &Opts)
193     : OutputFile(Opts.OutputFile), Targets(Opts.Targets),
194       IncludeSystemHeaders(Opts.IncludeSystemHeaders),
195       PhonyTarget(Opts.UsePhonyTargets),
196       AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),
197       IncludeModuleFiles(Opts.IncludeModuleFiles),
198       OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
199   for (const auto &ExtraDep : Opts.ExtraDeps) {
200     if (addDependency(ExtraDep.first))
201       ++InputFileIndex;
202   }
203 }
204 
205 void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
206   // Disable the "file not found" diagnostic if the -MG option was given.
207   if (AddMissingHeaderDeps)
208     PP.SetSuppressIncludeNotFoundError(true);
209 
210   DependencyCollector::attachToPreprocessor(PP);
211 }
212 
213 bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
214                                             bool IsSystem, bool IsModuleFile,
215                                             bool IsMissing) {
216   if (IsMissing) {
217     // Handle the case of missing file from an inclusion directive.
218     if (AddMissingHeaderDeps)
219       return true;
220     SeenMissingHeader = true;
221     return false;
222   }
223   if (IsModuleFile && !IncludeModuleFiles)
224     return false;
225 
226   if (isSpecialFilename(Filename))
227     return false;
228 
229   if (IncludeSystemHeaders)
230     return true;
231 
232   return !IsSystem;
233 }
234 
235 void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
236   outputDependencyFile(Diags);
237 }
238 
239 /// Print the filename, with escaping or quoting that accommodates the three
240 /// most likely tools that use dependency files: GNU Make, BSD Make, and
241 /// NMake/Jom.
242 ///
243 /// BSD Make is the simplest case: It does no escaping at all.  This means
244 /// characters that are normally delimiters, i.e. space and # (the comment
245 /// character) simply aren't supported in filenames.
246 ///
247 /// GNU Make does allow space and # in filenames, but to avoid being treated
248 /// as a delimiter or comment, these must be escaped with a backslash. Because
249 /// backslash is itself the escape character, if a backslash appears in a
250 /// filename, it should be escaped as well.  (As a special case, $ is escaped
251 /// as $$, which is the normal Make way to handle the $ character.)
252 /// For compatibility with BSD Make and historical practice, if GNU Make
253 /// un-escapes characters in a filename but doesn't find a match, it will
254 /// retry with the unmodified original string.
255 ///
256 /// GCC tries to accommodate both Make formats by escaping any space or #
257 /// characters in the original filename, but not escaping backslashes.  The
258 /// apparent intent is so that filenames with backslashes will be handled
259 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
260 /// unmodified original string; filenames with # or space characters aren't
261 /// supported by BSD Make at all, but will be handled correctly by GNU Make
262 /// due to the escaping.
263 ///
264 /// A corner case that GCC gets only partly right is when the original filename
265 /// has a backslash immediately followed by space or #.  GNU Make would expect
266 /// this backslash to be escaped; however GCC escapes the original backslash
267 /// only when followed by space, not #.  It will therefore take a dependency
268 /// from a directive such as
269 ///     #include "a\ b\#c.h"
270 /// and emit it as
271 ///     a\\\ b\\#c.h
272 /// which GNU Make will interpret as
273 ///     a\ b\
274 /// followed by a comment. Failing to find this file, it will fall back to the
275 /// original string, which probably doesn't exist either; in any case it won't
276 /// find
277 ///     a\ b\#c.h
278 /// which is the actual filename specified by the include directive.
279 ///
280 /// Clang does what GCC does, rather than what GNU Make expects.
281 ///
282 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
283 /// double-quotes to avoid misinterpreting them; see
284 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
285 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
286 /// for Windows file-naming info.
287 static void PrintFilename(raw_ostream &OS, StringRef Filename,
288                           DependencyOutputFormat OutputFormat) {
289   // Convert filename to platform native path
290   llvm::SmallString<256> NativePath;
291   llvm::sys::path::native(Filename.str(), NativePath);
292 
293   if (OutputFormat == DependencyOutputFormat::NMake) {
294     // Add quotes if needed. These are the characters listed as "special" to
295     // NMake, that are legal in a Windows filespec, and that could cause
296     // misinterpretation of the dependency string.
297     if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
298       OS << '\"' << NativePath << '\"';
299     else
300       OS << NativePath;
301     return;
302   }
303   assert(OutputFormat == DependencyOutputFormat::Make);
304   for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
305     if (NativePath[i] == '#') // Handle '#' the broken gcc way.
306       OS << '\\';
307     else if (NativePath[i] == ' ') { // Handle space correctly.
308       OS << '\\';
309       unsigned j = i;
310       while (j > 0 && NativePath[--j] == '\\')
311         OS << '\\';
312     } else if (NativePath[i] == '$') // $ is escaped by $$.
313       OS << '$';
314     OS << NativePath[i];
315   }
316 }
317 
318 void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
319   if (SeenMissingHeader) {
320     llvm::sys::fs::remove(OutputFile);
321     return;
322   }
323 
324   std::error_code EC;
325   llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
326   if (EC) {
327     Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();
328     return;
329   }
330 
331   outputDependencyFile(OS);
332 }
333 
334 void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
335   // Write out the dependency targets, trying to avoid overly long
336   // lines when possible. We try our best to emit exactly the same
337   // dependency file as GCC>=10, assuming the included files are the
338   // same.
339   const unsigned MaxColumns = 75;
340   unsigned Columns = 0;
341 
342   for (StringRef Target : Targets) {
343     unsigned N = Target.size();
344     if (Columns == 0) {
345       Columns += N;
346     } else if (Columns + N + 2 > MaxColumns) {
347       Columns = N + 2;
348       OS << " \\\n  ";
349     } else {
350       Columns += N + 1;
351       OS << ' ';
352     }
353     // Targets already quoted as needed.
354     OS << Target;
355   }
356 
357   OS << ':';
358   Columns += 1;
359 
360   // Now add each dependency in the order it was seen, but avoiding
361   // duplicates.
362   ArrayRef<std::string> Files = getDependencies();
363   for (StringRef File : Files) {
364     if (File == "<stdin>")
365       continue;
366     // Start a new line if this would exceed the column limit. Make
367     // sure to leave space for a trailing " \" in case we need to
368     // break the line on the next iteration.
369     unsigned N = File.size();
370     if (Columns + (N + 1) + 2 > MaxColumns) {
371       OS << " \\\n ";
372       Columns = 2;
373     }
374     OS << ' ';
375     PrintFilename(OS, File, OutputFormat);
376     Columns += N + 1;
377   }
378   OS << '\n';
379 
380   // Create phony targets if requested.
381   if (PhonyTarget && !Files.empty()) {
382     unsigned Index = 0;
383     for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
384       if (Index++ == InputFileIndex)
385         continue;
386       PrintFilename(OS, *I, OutputFormat);
387       OS << ":\n";
388     }
389   }
390 }
391