1 //===--- 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 14 #include "clang/Frontend/Utils.h" 15 #include "clang/Basic/FileManager.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "clang/Frontend/DependencyOutputOptions.h" 18 #include "clang/Frontend/FrontendDiagnostic.h" 19 #include "clang/Lex/DirectoryLookup.h" 20 #include "clang/Lex/LexDiagnostic.h" 21 #include "clang/Lex/PPCallbacks.h" 22 #include "clang/Lex/Preprocessor.h" 23 #include "clang/Serialization/ASTReader.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/ADT/StringSwitch.h" 26 #include "llvm/Support/FileSystem.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/raw_ostream.h" 29 30 using namespace clang; 31 32 namespace { 33 struct 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 53 StringRef Filename = FE->getName(); 54 55 // Remove leading "./" (or ".//" or "././" etc.) 56 while (Filename.size() > 2 && Filename[0] == '.' && 57 llvm::sys::path::is_separator(Filename[1])) { 58 Filename = Filename.substr(1); 59 while (llvm::sys::path::is_separator(Filename[0])) 60 Filename = Filename.substr(1); 61 } 62 63 DepCollector.maybeAddDependency(Filename, /*FromModule*/false, 64 FileType != SrcMgr::C_User, 65 /*IsModuleFile*/false, /*IsMissing*/false); 66 } 67 68 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 69 StringRef FileName, bool IsAngled, 70 CharSourceRange FilenameRange, const FileEntry *File, 71 StringRef SearchPath, StringRef RelativePath, 72 const Module *Imported) override { 73 if (!File) 74 DepCollector.maybeAddDependency(FileName, /*FromModule*/false, 75 /*IsSystem*/false, /*IsModuleFile*/false, 76 /*IsMissing*/true); 77 // Files that actually exist are handled by FileChanged. 78 } 79 80 void EndOfMainFile() override { 81 DepCollector.finishedMainFile(); 82 } 83 }; 84 85 struct DepCollectorASTListener : public ASTReaderListener { 86 DependencyCollector &DepCollector; 87 DepCollectorASTListener(DependencyCollector &L) : DepCollector(L) { } 88 bool needsInputFileVisitation() override { return true; } 89 bool needsSystemInputFileVisitation() override { 90 return DepCollector.needSystemDependencies(); 91 } 92 void visitModuleFile(StringRef Filename) override { 93 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, 94 /*IsSystem*/false, /*IsModuleFile*/true, 95 /*IsMissing*/false); 96 } 97 bool visitInputFile(StringRef Filename, bool IsSystem, 98 bool IsOverridden) override { 99 if (IsOverridden) 100 return true; 101 102 DepCollector.maybeAddDependency(Filename, /*FromModule*/true, IsSystem, 103 /*IsModuleFile*/false, /*IsMissing*/false); 104 return true; 105 } 106 }; 107 } // end anonymous namespace 108 109 void DependencyCollector::maybeAddDependency(StringRef Filename, bool FromModule, 110 bool IsSystem, bool IsModuleFile, 111 bool IsMissing) { 112 if (Seen.insert(Filename).second && 113 sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing)) 114 Dependencies.push_back(Filename); 115 } 116 117 static bool isSpecialFilename(StringRef Filename) { 118 return llvm::StringSwitch<bool>(Filename) 119 .Case("<built-in>", true) 120 .Case("<stdin>", true) 121 .Default(false); 122 } 123 124 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, 125 bool IsSystem, bool IsModuleFile, 126 bool IsMissing) { 127 return !isSpecialFilename(Filename) && 128 (needSystemDependencies() || !IsSystem); 129 } 130 131 DependencyCollector::~DependencyCollector() { } 132 void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { 133 PP.addPPCallbacks( 134 llvm::make_unique<DepCollectorPPCallbacks>(*this, PP.getSourceManager())); 135 } 136 void DependencyCollector::attachToASTReader(ASTReader &R) { 137 R.addListener(llvm::make_unique<DepCollectorASTListener>(*this)); 138 } 139 140 namespace { 141 /// Private implementation for DependencyFileGenerator 142 class DFGImpl : public PPCallbacks { 143 std::vector<std::string> Files; 144 llvm::StringSet<> FilesSet; 145 const Preprocessor *PP; 146 std::string OutputFile; 147 std::vector<std::string> Targets; 148 bool IncludeSystemHeaders; 149 bool PhonyTarget; 150 bool AddMissingHeaderDeps; 151 bool SeenMissingHeader; 152 bool IncludeModuleFiles; 153 DependencyOutputFormat OutputFormat; 154 155 private: 156 bool FileMatchesDepCriteria(const char *Filename, 157 SrcMgr::CharacteristicKind FileType); 158 void OutputDependencyFile(); 159 160 public: 161 DFGImpl(const Preprocessor *_PP, const DependencyOutputOptions &Opts) 162 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets), 163 IncludeSystemHeaders(Opts.IncludeSystemHeaders), 164 PhonyTarget(Opts.UsePhonyTargets), 165 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), 166 SeenMissingHeader(false), 167 IncludeModuleFiles(Opts.IncludeModuleFiles), 168 OutputFormat(Opts.OutputFormat) {} 169 170 void FileChanged(SourceLocation Loc, FileChangeReason Reason, 171 SrcMgr::CharacteristicKind FileType, 172 FileID PrevFID) override; 173 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 174 StringRef FileName, bool IsAngled, 175 CharSourceRange FilenameRange, const FileEntry *File, 176 StringRef SearchPath, StringRef RelativePath, 177 const Module *Imported) override; 178 179 void EndOfMainFile() override { 180 OutputDependencyFile(); 181 } 182 183 void AddFilename(StringRef Filename); 184 bool includeSystemHeaders() const { return IncludeSystemHeaders; } 185 bool includeModuleFiles() const { return IncludeModuleFiles; } 186 }; 187 188 class DFGASTReaderListener : public ASTReaderListener { 189 DFGImpl &Parent; 190 public: 191 DFGASTReaderListener(DFGImpl &Parent) 192 : Parent(Parent) { } 193 bool needsInputFileVisitation() override { return true; } 194 bool needsSystemInputFileVisitation() override { 195 return Parent.includeSystemHeaders(); 196 } 197 void visitModuleFile(StringRef Filename) override; 198 bool visitInputFile(StringRef Filename, bool isSystem, 199 bool isOverridden) override; 200 }; 201 } 202 203 DependencyFileGenerator::DependencyFileGenerator(void *Impl) 204 : Impl(Impl) { } 205 206 DependencyFileGenerator *DependencyFileGenerator::CreateAndAttachToPreprocessor( 207 clang::Preprocessor &PP, const clang::DependencyOutputOptions &Opts) { 208 209 if (Opts.Targets.empty()) { 210 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT); 211 return nullptr; 212 } 213 214 // Disable the "file not found" diagnostic if the -MG option was given. 215 if (Opts.AddMissingHeaderDeps) 216 PP.SetSuppressIncludeNotFoundError(true); 217 218 DFGImpl *Callback = new DFGImpl(&PP, Opts); 219 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callback)); 220 return new DependencyFileGenerator(Callback); 221 } 222 223 void DependencyFileGenerator::AttachToASTReader(ASTReader &R) { 224 DFGImpl *I = reinterpret_cast<DFGImpl *>(Impl); 225 assert(I && "missing implementation"); 226 R.addListener(llvm::make_unique<DFGASTReaderListener>(*I)); 227 } 228 229 /// FileMatchesDepCriteria - Determine whether the given Filename should be 230 /// considered as a dependency. 231 bool DFGImpl::FileMatchesDepCriteria(const char *Filename, 232 SrcMgr::CharacteristicKind FileType) { 233 if (isSpecialFilename(Filename)) 234 return false; 235 236 if (IncludeSystemHeaders) 237 return true; 238 239 return FileType == SrcMgr::C_User; 240 } 241 242 void DFGImpl::FileChanged(SourceLocation Loc, 243 FileChangeReason Reason, 244 SrcMgr::CharacteristicKind FileType, 245 FileID PrevFID) { 246 if (Reason != PPCallbacks::EnterFile) 247 return; 248 249 // Dependency generation really does want to go all the way to the 250 // file entry for a source location to find out what is depended on. 251 // We do not want #line markers to affect dependency generation! 252 SourceManager &SM = PP->getSourceManager(); 253 254 const FileEntry *FE = 255 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc))); 256 if (!FE) return; 257 258 StringRef Filename = FE->getName(); 259 if (!FileMatchesDepCriteria(Filename.data(), FileType)) 260 return; 261 262 // Remove leading "./" (or ".//" or "././" etc.) 263 while (Filename.size() > 2 && Filename[0] == '.' && 264 llvm::sys::path::is_separator(Filename[1])) { 265 Filename = Filename.substr(1); 266 while (llvm::sys::path::is_separator(Filename[0])) 267 Filename = Filename.substr(1); 268 } 269 270 AddFilename(Filename); 271 } 272 273 void DFGImpl::InclusionDirective(SourceLocation HashLoc, 274 const Token &IncludeTok, 275 StringRef FileName, 276 bool IsAngled, 277 CharSourceRange FilenameRange, 278 const FileEntry *File, 279 StringRef SearchPath, 280 StringRef RelativePath, 281 const Module *Imported) { 282 if (!File) { 283 if (AddMissingHeaderDeps) 284 AddFilename(FileName); 285 else 286 SeenMissingHeader = true; 287 } 288 } 289 290 void DFGImpl::AddFilename(StringRef Filename) { 291 if (FilesSet.insert(Filename).second) 292 Files.push_back(Filename); 293 } 294 295 /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or 296 /// other scary characters. NMake/Jom has a different set of scary characters, 297 /// but wraps filespecs in double-quotes to avoid misinterpreting them; 298 /// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info, 299 /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx 300 /// for Windows file-naming info. 301 static void PrintFilename(raw_ostream &OS, StringRef Filename, 302 DependencyOutputFormat OutputFormat) { 303 if (OutputFormat == DependencyOutputFormat::NMake) { 304 // Add quotes if needed. These are the characters listed as "special" to 305 // NMake, that are legal in a Windows filespec, and that could cause 306 // misinterpretation of the dependency string. 307 if (Filename.find_first_of(" #${}^!") != StringRef::npos) 308 OS << '\"' << Filename << '\"'; 309 else 310 OS << Filename; 311 return; 312 } 313 for (unsigned i = 0, e = Filename.size(); i != e; ++i) { 314 if (Filename[i] == ' ' || Filename[i] == '#') 315 OS << '\\'; 316 else if (Filename[i] == '$') // $ is escaped by $$. 317 OS << '$'; 318 OS << Filename[i]; 319 } 320 } 321 322 void DFGImpl::OutputDependencyFile() { 323 if (SeenMissingHeader) { 324 llvm::sys::fs::remove(OutputFile); 325 return; 326 } 327 328 std::error_code EC; 329 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text); 330 if (EC) { 331 PP->getDiagnostics().Report(diag::err_fe_error_opening) << OutputFile 332 << EC.message(); 333 return; 334 } 335 336 // Write out the dependency targets, trying to avoid overly long 337 // lines when possible. We try our best to emit exactly the same 338 // dependency file as GCC (4.2), assuming the included files are the 339 // same. 340 const unsigned MaxColumns = 75; 341 unsigned Columns = 0; 342 343 for (std::vector<std::string>::iterator 344 I = Targets.begin(), E = Targets.end(); I != E; ++I) { 345 unsigned N = I->length(); 346 if (Columns == 0) { 347 Columns += N; 348 } else if (Columns + N + 2 > MaxColumns) { 349 Columns = N + 2; 350 OS << " \\\n "; 351 } else { 352 Columns += N + 1; 353 OS << ' '; 354 } 355 // Targets already quoted as needed. 356 OS << *I; 357 } 358 359 OS << ':'; 360 Columns += 1; 361 362 // Now add each dependency in the order it was seen, but avoiding 363 // duplicates. 364 for (std::vector<std::string>::iterator I = Files.begin(), 365 E = Files.end(); I != E; ++I) { 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 = I->length(); 370 if (Columns + (N + 1) + 2 > MaxColumns) { 371 OS << " \\\n "; 372 Columns = 2; 373 } 374 OS << ' '; 375 PrintFilename(OS, *I, OutputFormat); 376 Columns += N + 1; 377 } 378 OS << '\n'; 379 380 // Create phony targets if requested. 381 if (PhonyTarget && !Files.empty()) { 382 // Skip the first entry, this is always the input file itself. 383 for (std::vector<std::string>::iterator I = Files.begin() + 1, 384 E = Files.end(); I != E; ++I) { 385 OS << '\n'; 386 PrintFilename(OS, *I, OutputFormat); 387 OS << ":\n"; 388 } 389 } 390 } 391 392 bool DFGASTReaderListener::visitInputFile(llvm::StringRef Filename, 393 bool IsSystem, bool IsOverridden) { 394 assert(!IsSystem || needsSystemInputFileVisitation()); 395 if (IsOverridden) 396 return true; 397 398 Parent.AddFilename(Filename); 399 return true; 400 } 401 402 void DFGASTReaderListener::visitModuleFile(llvm::StringRef Filename) { 403 if (Parent.includeModuleFiles()) 404 Parent.AddFilename(Filename); 405 } 406