xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/DependencyFile.cpp (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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/ADT/StringSwitch.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
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 (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                           Optional<FileEntryRef> 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                   Optional<FileEntryRef> 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   DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { }
113   bool needsInputFileVisitation() override { return true; }
114   bool needsSystemInputFileVisitation() override {
115     return DepCollector.needSystemDependencies();
116   }
117   void visitModuleFile(StringRef Filename,
118                        serialization::ModuleKind Kind) override {
119     DepCollector.maybeAddDependency(Filename, /*FromModule*/true,
120                                    /*IsSystem*/false, /*IsModuleFile*/true,
121                                    /*IsMissing*/false);
122   }
123   bool visitInputFile(StringRef Filename, bool IsSystem,
124                       bool IsOverridden, bool IsExplicitModule) override {
125     if (IsOverridden || IsExplicitModule)
126       return true;
127 
128     DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem,
129                                    /*IsModuleFile*/false, /*IsMissing*/false);
130     return true;
131   }
132 };
133 } // end anonymous namespace
134 
135 void DependencyCollector::maybeAddDependency(StringRef Filename,
136                                              bool FromModule, bool IsSystem,
137                                              bool IsModuleFile,
138                                              bool IsMissing) {
139   if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))
140     addDependency(Filename);
141 }
142 
143 bool DependencyCollector::addDependency(StringRef Filename) {
144   StringRef SearchPath;
145 #ifdef _WIN32
146   // Make the search insensitive to case and separators.
147   llvm::SmallString<256> TmpPath = Filename;
148   llvm::sys::path::native(TmpPath);
149   std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower);
150   SearchPath = TmpPath.str();
151 #else
152   SearchPath = Filename;
153 #endif
154 
155   if (Seen.insert(SearchPath).second) {
156     Dependencies.push_back(std::string(Filename));
157     return true;
158   }
159   return false;
160 }
161 
162 static bool isSpecialFilename(StringRef Filename) {
163   return llvm::StringSwitch<bool>(Filename)
164       .Case("<built-in>", true)
165       .Case("<stdin>", true)
166       .Default(false);
167 }
168 
169 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
170                                         bool IsSystem, bool IsModuleFile,
171                                         bool IsMissing) {
172   return !isSpecialFilename(Filename) &&
173          (needSystemDependencies() || !IsSystem);
174 }
175 
176 DependencyCollector::~DependencyCollector() { }
177 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {
178   PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*this, PP));
179   PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
180       std::make_unique<DepCollectorMMCallbacks>(*this));
181 }
182 void DependencyCollector::attachToASTReader(ASTReader &R) {
183   R.addListener(std::make_unique<DepCollectorASTListener>(*this));
184 }
185 
186 DependencyFileGenerator::DependencyFileGenerator(
187     const DependencyOutputOptions &Opts)
188     : OutputFile(Opts.OutputFile), Targets(Opts.Targets),
189       IncludeSystemHeaders(Opts.IncludeSystemHeaders),
190       PhonyTarget(Opts.UsePhonyTargets),
191       AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),
192       IncludeModuleFiles(Opts.IncludeModuleFiles),
193       OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
194   for (const auto &ExtraDep : Opts.ExtraDeps) {
195     if (addDependency(ExtraDep.first))
196       ++InputFileIndex;
197   }
198 }
199 
200 void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {
201   // Disable the "file not found" diagnostic if the -MG option was given.
202   if (AddMissingHeaderDeps)
203     PP.SetSuppressIncludeNotFoundError(true);
204 
205   DependencyCollector::attachToPreprocessor(PP);
206 }
207 
208 bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,
209                                             bool IsSystem, bool IsModuleFile,
210                                             bool IsMissing) {
211   if (IsMissing) {
212     // Handle the case of missing file from an inclusion directive.
213     if (AddMissingHeaderDeps)
214       return true;
215     SeenMissingHeader = true;
216     return false;
217   }
218   if (IsModuleFile && !IncludeModuleFiles)
219     return false;
220 
221   if (isSpecialFilename(Filename))
222     return false;
223 
224   if (IncludeSystemHeaders)
225     return true;
226 
227   return !IsSystem;
228 }
229 
230 void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {
231   outputDependencyFile(Diags);
232 }
233 
234 /// Print the filename, with escaping or quoting that accommodates the three
235 /// most likely tools that use dependency files: GNU Make, BSD Make, and
236 /// NMake/Jom.
237 ///
238 /// BSD Make is the simplest case: It does no escaping at all.  This means
239 /// characters that are normally delimiters, i.e. space and # (the comment
240 /// character) simply aren't supported in filenames.
241 ///
242 /// GNU Make does allow space and # in filenames, but to avoid being treated
243 /// as a delimiter or comment, these must be escaped with a backslash. Because
244 /// backslash is itself the escape character, if a backslash appears in a
245 /// filename, it should be escaped as well.  (As a special case, $ is escaped
246 /// as $$, which is the normal Make way to handle the $ character.)
247 /// For compatibility with BSD Make and historical practice, if GNU Make
248 /// un-escapes characters in a filename but doesn't find a match, it will
249 /// retry with the unmodified original string.
250 ///
251 /// GCC tries to accommodate both Make formats by escaping any space or #
252 /// characters in the original filename, but not escaping backslashes.  The
253 /// apparent intent is so that filenames with backslashes will be handled
254 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the
255 /// unmodified original string; filenames with # or space characters aren't
256 /// supported by BSD Make at all, but will be handled correctly by GNU Make
257 /// due to the escaping.
258 ///
259 /// A corner case that GCC gets only partly right is when the original filename
260 /// has a backslash immediately followed by space or #.  GNU Make would expect
261 /// this backslash to be escaped; however GCC escapes the original backslash
262 /// only when followed by space, not #.  It will therefore take a dependency
263 /// from a directive such as
264 ///     #include "a\ b\#c.h"
265 /// and emit it as
266 ///     a\\\ b\\#c.h
267 /// which GNU Make will interpret as
268 ///     a\ b\
269 /// followed by a comment. Failing to find this file, it will fall back to the
270 /// original string, which probably doesn't exist either; in any case it won't
271 /// find
272 ///     a\ b\#c.h
273 /// which is the actual filename specified by the include directive.
274 ///
275 /// Clang does what GCC does, rather than what GNU Make expects.
276 ///
277 /// NMake/Jom has a different set of scary characters, but wraps filespecs in
278 /// double-quotes to avoid misinterpreting them; see
279 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,
280 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
281 /// for Windows file-naming info.
282 static void PrintFilename(raw_ostream &OS, StringRef Filename,
283                           DependencyOutputFormat OutputFormat) {
284   // Convert filename to platform native path
285   llvm::SmallString<256> NativePath;
286   llvm::sys::path::native(Filename.str(), NativePath);
287 
288   if (OutputFormat == DependencyOutputFormat::NMake) {
289     // Add quotes if needed. These are the characters listed as "special" to
290     // NMake, that are legal in a Windows filespec, and that could cause
291     // misinterpretation of the dependency string.
292     if (NativePath.find_first_of(" #${}^!") != StringRef::npos)
293       OS << '\"' << NativePath << '\"';
294     else
295       OS << NativePath;
296     return;
297   }
298   assert(OutputFormat == DependencyOutputFormat::Make);
299   for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {
300     if (NativePath[i] == '#') // Handle '#' the broken gcc way.
301       OS << '\\';
302     else if (NativePath[i] == ' ') { // Handle space correctly.
303       OS << '\\';
304       unsigned j = i;
305       while (j > 0 && NativePath[--j] == '\\')
306         OS << '\\';
307     } else if (NativePath[i] == '$') // $ is escaped by $$.
308       OS << '$';
309     OS << NativePath[i];
310   }
311 }
312 
313 void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {
314   if (SeenMissingHeader) {
315     llvm::sys::fs::remove(OutputFile);
316     return;
317   }
318 
319   std::error_code EC;
320   llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
321   if (EC) {
322     Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();
323     return;
324   }
325 
326   outputDependencyFile(OS);
327 }
328 
329 void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
330   // Write out the dependency targets, trying to avoid overly long
331   // lines when possible. We try our best to emit exactly the same
332   // dependency file as GCC (4.2), assuming the included files are the
333   // same.
334   const unsigned MaxColumns = 75;
335   unsigned Columns = 0;
336 
337   for (StringRef Target : Targets) {
338     unsigned N = Target.size();
339     if (Columns == 0) {
340       Columns += N;
341     } else if (Columns + N + 2 > MaxColumns) {
342       Columns = N + 2;
343       OS << " \\\n  ";
344     } else {
345       Columns += N + 1;
346       OS << ' ';
347     }
348     // Targets already quoted as needed.
349     OS << Target;
350   }
351 
352   OS << ':';
353   Columns += 1;
354 
355   // Now add each dependency in the order it was seen, but avoiding
356   // duplicates.
357   ArrayRef<std::string> Files = getDependencies();
358   for (StringRef File : Files) {
359     // Start a new line if this would exceed the column limit. Make
360     // sure to leave space for a trailing " \" in case we need to
361     // break the line on the next iteration.
362     unsigned N = File.size();
363     if (Columns + (N + 1) + 2 > MaxColumns) {
364       OS << " \\\n ";
365       Columns = 2;
366     }
367     OS << ' ';
368     PrintFilename(OS, File, OutputFormat);
369     Columns += N + 1;
370   }
371   OS << '\n';
372 
373   // Create phony targets if requested.
374   if (PhonyTarget && !Files.empty()) {
375     unsigned Index = 0;
376     for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {
377       if (Index++ == InputFileIndex)
378         continue;
379       OS << '\n';
380       PrintFilename(OS, *I, OutputFormat);
381       OS << ":\n";
382     }
383   }
384 }
385