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 SourceManager &SM; 35 DepCollectorPPCallbacks(DependencyCollector &L, SourceManager &SM) 36 : DepCollector(L), SM(SM) { } 37 38 void FileChanged(SourceLocation Loc, FileChangeReason Reason, 39 SrcMgr::CharacteristicKind FileType, 40 FileID PrevFID) override { 41 if (Reason != PPCallbacks::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 const FileEntry *FE = 48 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); 49 if (!FE) 50 return; 51 52 StringRef Filename = 53 llvm::sys::path::remove_leading_dotslash(FE->getName()); 54 55 DepCollector.maybeAddDependency(Filename, /*FromModule*/false, 56 isSystem(FileType), 57 /*IsModuleFile*/false, /*IsMissing*/false); 58 } 59 60 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 61 StringRef FileName, bool IsAngled, 62 CharSourceRange FilenameRange, const FileEntry *File, 63 StringRef SearchPath, StringRef RelativePath, 64 const Module *Imported, 65 SrcMgr::CharacteristicKind FileType) override { 66 if (!File) 67 DepCollector.maybeAddDependency(FileName, /*FromModule*/false, 68 /*IsSystem*/false, /*IsModuleFile*/false, 69 /*IsMissing*/true); 70 // Files that actually exist are handled by FileChanged. 71 } 72 73 void EndOfMainFile() override { 74 DepCollector.finishedMainFile(); 75 } 76 }; 77 78 struct DepCollectorMMCallbacks : public ModuleMapCallbacks { 79 DependencyCollector &DepCollector; 80 DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {} 81 82 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry, 83 bool IsSystem) override { 84 StringRef Filename = Entry.getName(); 85 DepCollector.maybeAddDependency(Filename, /*FromModule*/false, 86 /*IsSystem*/IsSystem, 87 /*IsModuleFile*/false, 88 /*IsMissing*/false); 89 } 90 }; 91 92 struct DepCollectorASTListener : public ASTReaderListener { 93 DependencyCollector &DepCollector; 94 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } 95 bool needsInputFileVisitation() override { return true; } 96 bool needsSystemInputFileVisitation() override { 97 return DepCollector.needSystemDependencies(); 98 } 99 void visitModuleFile(StringRef Filename, 100 serialization::ModuleKind Kind) override { 101 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, 102 /*IsSystem*/false, /*IsModuleFile*/true, 103 /*IsMissing*/false); 104 } 105 bool visitInputFile(StringRef Filename, bool IsSystem, 106 bool IsOverridden, bool IsExplicitModule) override { 107 if (IsOverridden || IsExplicitModule) 108 return true; 109 110 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, 111 /*IsModuleFile*/false, /*IsMissing*/false); 112 return true; 113 } 114 }; 115 } // end anonymous namespace 116 117 void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule, 118 bool IsSystem, bool IsModuleFile, 119 bool IsMissing) { 120 if (Seen.insert(Filename).second && 121 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) 122 Dependencies.push_back(Filename); 123 } 124 125 static bool isSpecialFilename(StringRef Filename) { 126 return llvm::StringSwitch<bool>(Filename) 127 .Case("<built-in>", true) 128 .Case("<stdin>", true) 129 .Default(false); 130 } 131 132 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, 133 bool IsSystem, bool IsModuleFile, 134 bool IsMissing) { 135 return !isSpecialFilename(Filename) && 136 (needSystemDependencies() || !IsSystem); 137 } 138 139 DependencyCollector::~DependencyCollector() { } 140 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { 141 PP.addPPCallbacks( 142 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager())); 143 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 144 llvm::make_unique<DepCollectorMMCallbacks>(*this)); 145 } 146 void DependencyCollector::attachToASTReader(ASTReader &R) { 147 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this)); 148 } 149 150 namespace { 151 /// Private implementation for DependencyFileGenerator 152 class DFGImpl : public PPCallbacks { 153 std::vector<std::string> Files; 154 llvm::StringSet<> FilesSet; 155 const Preprocessor *PP; 156 std::string OutputFile; 157 std::vector<std::string> Targets; 158 bool IncludeSystemHeaders; 159 bool PhonyTarget; 160 bool AddMissingHeaderDeps; 161 bool SeenMissingHeader; 162 bool IncludeModuleFiles; 163 DependencyOutputFormat OutputFormat; 164 unsigned InputFileIndex; 165 166 private: 167 bool FileMatchesDepCriteria(const char *Filename, 168 SrcMgr::CharacteristicKind FileType); 169 void OutputDependencyFile(); 170 171 public: 172 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts) 173 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), 174 IncludeSystemHeaders(Opts.IncludeSystemHeaders), 175 PhonyTarget(Opts.UsePhonyTargets), 176 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), 177 SeenMissingHeader(false), 178 IncludeModuleFiles(Opts.IncludeModuleFiles), 179 OutputFormat(Opts.OutputFormat), 180 InputFileIndex(0) { 181 for (const auto &ExtraDep : Opts.ExtraDeps) { 182 if (AddFilename(ExtraDep)) 183 ++InputFileIndex; 184 } 185 } 186 187 void FileChanged(SourceLocation Loc, FileChangeReason Reason, 188 SrcMgr::CharacteristicKind FileType, 189 FileID PrevFID) override; 190 191 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok, 192 SrcMgr::CharacteristicKind FileType) override; 193 194 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 195 StringRef FileName, bool IsAngled, 196 CharSourceRange FilenameRange, const FileEntry *File, 197 StringRef SearchPath, StringRef RelativePath, 198 const Module *Imported, 199 SrcMgr::CharacteristicKind FileType) override; 200 201 void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, 202 const FileEntry *File, 203 SrcMgr::CharacteristicKind FileType) override; 204 205 void EndOfMainFile() override { 206 OutputDependencyFile(); 207 } 208 209 bool AddFilename(StringRef Filename); 210 bool includeSystemHeaders() const { return IncludeSystemHeaders; } 211 bool includeModuleFiles() const { return IncludeModuleFiles; } 212 }; 213 214 class DFGMMCallback : public ModuleMapCallbacks { 215 DFGImpl &Parent; 216 public: 217 DFGMMCallback(DFGImpl &Parent) : Parent(Parent) {} 218 void moduleMapFileRead(SourceLocation Loc, const FileEntry &Entry, 219 bool IsSystem) override { 220 if (!IsSystem || Parent.includeSystemHeaders()) 221 Parent.AddFilename(Entry.getName()); 222 } 223 }; 224 225 class DFGASTReaderListener : public ASTReaderListener { 226 DFGImpl &Parent; 227 public: 228 DFGASTReaderListener(DFGImpl &Parent) 229 : Parent(Parent) { } 230 bool needsInputFileVisitation() override { return true; } 231 bool needsSystemInputFileVisitation() override { 232 return Parent.includeSystemHeaders(); 233 } 234 void visitModuleFile(StringRef Filename, 235 serialization::ModuleKind Kind) override; 236 bool visitInputFile(StringRef Filename, bool isSystem, 237 bool isOverridden, bool isExplicitModule) override; 238 }; 239 } 240 241 DependencyFileGenerator::DependencyFileGenerator(void *Impl) 242 : Impl(Impl) { } 243 244 DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( 245 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) { 246 247 if (Opts.Targets.empty()) { 248 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); 249 return nullptr; 250 } 251 252 // Disable the "file not found" diagnostic if the -MG option was given. 253 if (Opts.AddMissingHeaderDeps) 254 PP.SetSuppressIncludeNotFoundError(true); 255 256 DFGImpl *Callback = new DFGImpl(&PP, Opts); 257 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback)); 258 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 259 llvm::make_unique<DFGMMCallback>(*Callback)); 260 return new DependencyFileGenerator(Callback); 261 } 262 263 void DependencyFileGenerator::AttachToASTReader(ASTReader &R) { 264 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl); 265 assert(I && "missing implementation"); 266 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I)); 267 } 268 269 /// FileMatchesDepCriteria - Determine whether the given Filename should be 270 /// considered as a dependency. 271 bool DFGImpl::FileMatchesDepCriteria(const char *Filename, 272 SrcMgr::CharacteristicKind FileType) { 273 if (isSpecialFilename(Filename)) 274 return false; 275 276 if (IncludeSystemHeaders) 277 return true; 278 279 return !isSystem(FileType); 280 } 281 282 void DFGImpl::FileChanged(SourceLocation Loc, 283 FileChangeReason Reason, 284 SrcMgr::CharacteristicKind FileType, 285 FileID PrevFID) { 286 if (Reason != PPCallbacks::EnterFile) 287 return; 288 289 // Dependency generation really does want to go all the way to the 290 // file entry for a source location to find out what is depended on. 291 // We do not want #line markers to affect dependency generation! 292 SourceManager &SM = PP->getSourceManager(); 293 294 const FileEntry *FE = 295 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); 296 if (!FE) return; 297 298 StringRef Filename = FE->getName(); 299 if (!FileMatchesDepCriteria(Filename.data(), FileType)) 300 return; 301 302 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename)); 303 } 304 305 void DFGImpl::FileSkipped(const FileEntry &SkippedFile, 306 const Token &FilenameTok, 307 SrcMgr::CharacteristicKind FileType) { 308 StringRef Filename = SkippedFile.getName(); 309 if (!FileMatchesDepCriteria(Filename.data(), FileType)) 310 return; 311 312 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename)); 313 } 314 315 void DFGImpl::InclusionDirective(SourceLocation HashLoc, 316 const Token &IncludeTok, 317 StringRef FileName, 318 bool IsAngled, 319 CharSourceRange FilenameRange, 320 const FileEntry *File, 321 StringRef SearchPath, 322 StringRef RelativePath, 323 const Module *Imported, 324 SrcMgr::CharacteristicKind FileType) { 325 if (!File) { 326 if (AddMissingHeaderDeps) 327 AddFilename(FileName); 328 else 329 SeenMissingHeader = true; 330 } 331 } 332 333 void DFGImpl::HasInclude(SourceLocation Loc, StringRef SpelledFilename, 334 bool IsAngled, const FileEntry *File, 335 SrcMgr::CharacteristicKind FileType) { 336 if (!File) 337 return; 338 StringRef Filename = File->getName(); 339 if (!FileMatchesDepCriteria(Filename.data(), FileType)) 340 return; 341 AddFilename(llvm::sys::path::remove_leading_dotslash(Filename)); 342 } 343 344 bool DFGImpl::AddFilename(StringRef Filename) { 345 if (FilesSet.insert(Filename).second) { 346 Files.push_back(Filename); 347 return true; 348 } 349 return false; 350 } 351 352 /// Print the filename, with escaping or quoting that accommodates the three 353 /// most likely tools that use dependency files: GNU Make, BSD Make, and 354 /// NMake/Jom. 355 /// 356 /// BSD Make is the simplest case: It does no escaping at all. This means 357 /// characters that are normally delimiters, i.e. space and # (the comment 358 /// character) simply aren't supported in filenames. 359 /// 360 /// GNU Make does allow space and # in filenames, but to avoid being treated 361 /// as a delimiter or comment, these must be escaped with a backslash. Because 362 /// backslash is itself the escape character, if a backslash appears in a 363 /// filename, it should be escaped as well. (As a special case, $ is escaped 364 /// as $$, which is the normal Make way to handle the $ character.) 365 /// For compatibility with BSD Make and historical practice, if GNU Make 366 /// un-escapes characters in a filename but doesn't find a match, it will 367 /// retry with the unmodified original string. 368 /// 369 /// GCC tries to accommodate both Make formats by escaping any space or # 370 /// characters in the original filename, but not escaping backslashes. The 371 /// apparent intent is so that filenames with backslashes will be handled 372 /// correctly by BSD Make, and by GNU Make in its fallback mode of using the 373 /// unmodified original string; filenames with # or space characters aren't 374 /// supported by BSD Make at all, but will be handled correctly by GNU Make 375 /// due to the escaping. 376 /// 377 /// A corner case that GCC gets only partly right is when the original filename 378 /// has a backslash immediately followed by space or #. GNU Make would expect 379 /// this backslash to be escaped; however GCC escapes the original backslash 380 /// only when followed by space, not #. It will therefore take a dependency 381 /// from a directive such as 382 /// #include "a\ b\#c.h" 383 /// and emit it as 384 /// a\\\ b\\#c.h 385 /// which GNU Make will interpret as 386 /// a\ b\ 387 /// followed by a comment. Failing to find this file, it will fall back to the 388 /// original string, which probably doesn't exist either; in any case it won't 389 /// find 390 /// a\ b\#c.h 391 /// which is the actual filename specified by the include directive. 392 /// 393 /// Clang does what GCC does, rather than what GNU Make expects. 394 /// 395 /// NMake/Jom has a different set of scary characters, but wraps filespecs in 396 /// double-quotes to avoid misinterpreting them; see 397 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, 398 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx 399 /// for Windows file-naming info. 400 static void PrintFilename(raw_ostream &OS, StringRef Filename, 401 DependencyOutputFormat OutputFormat) { 402 // Convert filename to platform native path 403 llvm::SmallString<256> NativePath; 404 llvm::sys::path::native(Filename.str(), NativePath); 405 406 if (OutputFormat == DependencyOutputFormat::NMake) { 407 // Add quotes if needed. These are the characters listed as "special" to 408 // NMake, that are legal in a Windows filespec, and that could cause 409 // misinterpretation of the dependency string. 410 if (NativePath.find_first_of(" #${}^!") != StringRef::npos) 411 OS << '\"' << NativePath << '\"'; 412 else 413 OS << NativePath; 414 return; 415 } 416 assert(OutputFormat == DependencyOutputFormat::Make); 417 for (unsigned i = 0, e = NativePath.size(); i != e; ++i) { 418 if (NativePath[i] == '#') // Handle '#' the broken gcc way. 419 OS << '\\'; 420 else if (NativePath[i] == ' ') { // Handle space correctly. 421 OS << '\\'; 422 unsigned j = i; 423 while (j > 0 && NativePath[--j] == '\\') 424 OS << '\\'; 425 } else if (NativePath[i] == '$') // $ is escaped by $$. 426 OS << '$'; 427 OS << NativePath[i]; 428 } 429 } 430 431 void DFGImpl::OutputDependencyFile() { 432 if (SeenMissingHeader) { 433 llvm::sys::fs::remove(OutputFile); 434 return; 435 } 436 437 std::error_code EC; 438 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text); 439 if (EC) { 440 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile 441 << EC.message(); 442 return; 443 } 444 445 // Write out the dependency targets, trying to avoid overly long 446 // lines when possible. We try our best to emit exactly the same 447 // dependency file as GCC (4.2), assuming the included files are the 448 // same. 449 const unsigned MaxColumns = 75; 450 unsigned Columns = 0; 451 452 for (StringRef Target : Targets) { 453 unsigned N = Target.size(); 454 if (Columns == 0) { 455 Columns += N; 456 } else if (Columns + N + 2 > MaxColumns) { 457 Columns = N + 2; 458 OS << " \\\n "; 459 } else { 460 Columns += N + 1; 461 OS << ' '; 462 } 463 // Targets already quoted as needed. 464 OS << Target; 465 } 466 467 OS << ':'; 468 Columns += 1; 469 470 // Now add each dependency in the order it was seen, but avoiding 471 // duplicates. 472 for (StringRef File : Files) { 473 // Start a new line if this would exceed the column limit. Make 474 // sure to leave space for a trailing " \" in case we need to 475 // break the line on the next iteration. 476 unsigned N = File.size(); 477 if (Columns + (N + 1) + 2 > MaxColumns) { 478 OS << " \\\n "; 479 Columns = 2; 480 } 481 OS << ' '; 482 PrintFilename(OS, File, OutputFormat); 483 Columns += N + 1; 484 } 485 OS << '\n'; 486 487 // Create phony targets if requested. 488 if (PhonyTarget && !Files.empty()) { 489 unsigned Index = 0; 490 for (auto I = Files.begin(), E = Files.end(); I != E; ++I) { 491 if (Index++ == InputFileIndex) 492 continue; 493 OS << '\n'; 494 PrintFilename(OS, *I, OutputFormat); 495 OS << ":\n"; 496 } 497 } 498 } 499 500 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename, 501 bool IsSystem, bool IsOverridden, 502 bool IsExplicitModule) { 503 assert(!IsSystem || needsSystemInputFileVisitation()); 504 if (IsOverridden || IsExplicitModule) 505 return true; 506 507 Parent.AddFilename(Filename); 508 return true; 509 } 510 511 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename, 512 serialization::ModuleKind Kind) { 513 if (Parent.includeModuleFiles()) 514 Parent.AddFilename(Filename); 515 } 516