| 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/Basic/DiagnosticFrontend.h" |
| 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Basic/MakeSupport.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 18 | #include "clang/Frontend/Utils.h" |
| 19 | #include "clang/Lex/DirectoryLookup.h" |
| 20 | #include "clang/Lex/ModuleMap.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/Support/FileSystem.h" |
| 26 | #include "llvm/Support/Path.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <optional> |
| 29 | |
| 30 | using namespace clang; |
| 31 | |
| 32 | namespace { |
| 33 | struct DepCollectorPPCallbacks : public PPCallbacks { |
| 34 | DependencyCollector &DepCollector; |
| 35 | Preprocessor &PP; |
| 36 | DepCollectorPPCallbacks(DependencyCollector &L, Preprocessor &PP) |
| 37 | : DepCollector(L), PP(PP) {} |
| 38 | |
| 39 | void LexedFileChanged(FileID FID, LexedFileChangeReason Reason, |
| 40 | SrcMgr::CharacteristicKind FileType, FileID PrevFID, |
| 41 | SourceLocation Loc) override { |
| 42 | if (Reason != PPCallbacks::LexedFileChangeReason::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 | if (std::optional<StringRef> Filename = |
| 49 | PP.getSourceManager().getNonBuiltinFilenameForID(FID)) |
| 50 | DepCollector.maybeAddDependency( |
| 51 | Filename: llvm::sys::path::remove_leading_dotslash(path: *Filename), |
| 52 | /*FromModule*/ false, IsSystem: isSystem(CK: FileType), /*IsModuleFile*/ false, |
| 53 | /*IsDirectModuleImport*/ false, /*IsMissing*/ false); |
| 54 | } |
| 55 | |
| 56 | void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, |
| 57 | SrcMgr::CharacteristicKind FileType) override { |
| 58 | StringRef Filename = |
| 59 | llvm::sys::path::remove_leading_dotslash(path: SkippedFile.getName()); |
| 60 | DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, |
| 61 | /*IsSystem=*/isSystem(CK: FileType), |
| 62 | /*IsModuleFile=*/false, |
| 63 | /*IsDirectModuleImport=*/false, |
| 64 | /*IsMissing=*/false); |
| 65 | } |
| 66 | |
| 67 | void EmbedDirective(SourceLocation, StringRef, bool, |
| 68 | OptionalFileEntryRef File, |
| 69 | const LexEmbedParametersResult &) override { |
| 70 | assert(File && "expected to only be called when the file is found" ); |
| 71 | StringRef FileName = |
| 72 | llvm::sys::path::remove_leading_dotslash(path: File->getName()); |
| 73 | DepCollector.maybeAddDependency(Filename: FileName, |
| 74 | /*FromModule*/ false, |
| 75 | /*IsSystem*/ false, |
| 76 | /*IsModuleFile*/ false, |
| 77 | /*IsDirectModuleImport*/ false, |
| 78 | /*IsMissing*/ false); |
| 79 | } |
| 80 | |
| 81 | bool EmbedFileNotFound(StringRef FileName) override { |
| 82 | DepCollector.maybeAddDependency( |
| 83 | Filename: llvm::sys::path::remove_leading_dotslash(path: FileName), |
| 84 | /*FromModule=*/false, |
| 85 | /*IsSystem=*/false, |
| 86 | /*IsModuleFile=*/false, |
| 87 | /*IsDirectModuleImport=*/false, |
| 88 | /*IsMissing=*/true); |
| 89 | // Return true to silence the file not found diagnostic. |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 94 | StringRef FileName, bool IsAngled, |
| 95 | CharSourceRange FilenameRange, |
| 96 | OptionalFileEntryRef File, StringRef SearchPath, |
| 97 | StringRef RelativePath, const Module *SuggestedModule, |
| 98 | bool ModuleImported, |
| 99 | SrcMgr::CharacteristicKind FileType) override { |
| 100 | if (!File) |
| 101 | DepCollector.maybeAddDependency(Filename: FileName, /*FromModule*/ false, |
| 102 | /*IsSystem*/ false, |
| 103 | /*IsModuleFile*/ false, |
| 104 | /*IsDirectModuleImport*/ false, |
| 105 | /*IsMissing*/ true); |
| 106 | // Files that actually exist are handled by FileChanged. |
| 107 | } |
| 108 | |
| 109 | void HasEmbed(SourceLocation, StringRef, bool, |
| 110 | OptionalFileEntryRef File) override { |
| 111 | if (!File) |
| 112 | return; |
| 113 | StringRef Filename = |
| 114 | llvm::sys::path::remove_leading_dotslash(path: File->getName()); |
| 115 | DepCollector.maybeAddDependency(Filename, |
| 116 | /*FromModule=*/false, IsSystem: false, |
| 117 | /*IsModuleFile=*/false, |
| 118 | /*IsDirectModuleImport=*/false, |
| 119 | /*IsMissing=*/false); |
| 120 | } |
| 121 | |
| 122 | void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, |
| 123 | OptionalFileEntryRef File, |
| 124 | SrcMgr::CharacteristicKind FileType) override { |
| 125 | if (!File) |
| 126 | return; |
| 127 | StringRef Filename = |
| 128 | llvm::sys::path::remove_leading_dotslash(path: File->getName()); |
| 129 | DepCollector.maybeAddDependency(Filename, /*FromModule=*/false, |
| 130 | /*IsSystem=*/isSystem(CK: FileType), |
| 131 | /*IsModuleFile=*/false, |
| 132 | /*IsDirectModuleImport=*/false, |
| 133 | /*IsMissing=*/false); |
| 134 | } |
| 135 | |
| 136 | void EndOfMainFile() override { |
| 137 | DepCollector.finishedMainFile(Diags&: PP.getDiagnostics()); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | struct DepCollectorMMCallbacks : public ModuleMapCallbacks { |
| 142 | DependencyCollector &DepCollector; |
| 143 | DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {} |
| 144 | |
| 145 | void moduleMapFileRead(SourceLocation Loc, FileEntryRef Entry, |
| 146 | bool IsSystem) override { |
| 147 | StringRef Filename = Entry.getName(); |
| 148 | DepCollector.maybeAddDependency(Filename, /*FromModule*/ false, |
| 149 | /*IsSystem*/ IsSystem, |
| 150 | /*IsModuleFile*/ false, |
| 151 | /*IsDirectModuleImport*/ false, |
| 152 | /*IsMissing*/ false); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | struct DepCollectorASTListener : public ASTReaderListener { |
| 157 | DependencyCollector &DepCollector; |
| 158 | FileManager &FileMgr; |
| 159 | DepCollectorASTListener(DependencyCollector &L, FileManager &FileMgr) |
| 160 | : DepCollector(L), FileMgr(FileMgr) {} |
| 161 | bool needsInputFileVisitation() override { return true; } |
| 162 | bool needsSystemInputFileVisitation() override { |
| 163 | return DepCollector.needSystemDependencies(); |
| 164 | } |
| 165 | void visitModuleFile(ModuleFileName Filename, serialization::ModuleKind Kind, |
| 166 | bool DirectlyImported) override { |
| 167 | DepCollector.maybeAddDependency(Filename, /*FromModule*/ true, |
| 168 | /*IsSystem*/ false, /*IsModuleFile*/ true, |
| 169 | /*IsDirectModuleImport*/ DirectlyImported, |
| 170 | /*IsMissing*/ false); |
| 171 | } |
| 172 | bool visitInputFile(StringRef Filename, bool IsSystem, |
| 173 | bool IsOverridden, bool IsExplicitModule) override { |
| 174 | if (IsOverridden || IsExplicitModule) |
| 175 | return true; |
| 176 | |
| 177 | // Run this through the FileManager in order to respect 'use-external-name' |
| 178 | // in case we have a VFS overlay. |
| 179 | if (auto FE = FileMgr.getOptionalFileRef(Filename)) |
| 180 | Filename = FE->getName(); |
| 181 | |
| 182 | DepCollector.maybeAddDependency(Filename, /*FromModule*/ true, IsSystem, |
| 183 | /*IsModuleFile*/ false, |
| 184 | /*IsDirectModuleImport*/ false, |
| 185 | /*IsMissing*/ false); |
| 186 | return true; |
| 187 | } |
| 188 | }; |
| 189 | } // end anonymous namespace |
| 190 | |
| 191 | void DependencyCollector::maybeAddDependency(StringRef Filename, |
| 192 | bool FromModule, bool IsSystem, |
| 193 | bool IsModuleFile, |
| 194 | bool IsDirectModuleImport, |
| 195 | bool IsMissing) { |
| 196 | if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, |
| 197 | IsDirectModuleImport, IsMissing)) |
| 198 | addDependency(Filename); |
| 199 | } |
| 200 | |
| 201 | bool DependencyCollector::addDependency(StringRef Filename) { |
| 202 | StringRef SearchPath; |
| 203 | #ifdef _WIN32 |
| 204 | // Make the search insensitive to case and separators. |
| 205 | llvm::SmallString<256> TmpPath = Filename; |
| 206 | llvm::sys::path::native(TmpPath); |
| 207 | std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower); |
| 208 | SearchPath = TmpPath.str(); |
| 209 | #else |
| 210 | SearchPath = Filename; |
| 211 | #endif |
| 212 | |
| 213 | if (Seen.insert(key: SearchPath).second) { |
| 214 | Dependencies.push_back(x: std::string(Filename)); |
| 215 | return true; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | static bool isSpecialFilename(StringRef Filename) { |
| 221 | return Filename == "<built-in>" ; |
| 222 | } |
| 223 | |
| 224 | bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule, |
| 225 | bool IsSystem, bool IsModuleFile, |
| 226 | bool IsDirectModuleImport, |
| 227 | bool IsMissing) { |
| 228 | return !isSpecialFilename(Filename) && |
| 229 | (needSystemDependencies() || !IsSystem); |
| 230 | } |
| 231 | |
| 232 | DependencyCollector::~DependencyCollector() { } |
| 233 | void DependencyCollector::attachToPreprocessor(Preprocessor &PP) { |
| 234 | PP.addPPCallbacks(C: std::make_unique<DepCollectorPPCallbacks>(args&: *this, args&: PP)); |
| 235 | PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( |
| 236 | Callback: std::make_unique<DepCollectorMMCallbacks>(args&: *this)); |
| 237 | } |
| 238 | void DependencyCollector::attachToASTReader(ASTReader &R) { |
| 239 | R.addListener( |
| 240 | L: std::make_unique<DepCollectorASTListener>(args&: *this, args&: R.getFileManager())); |
| 241 | } |
| 242 | |
| 243 | DependencyFileGenerator::DependencyFileGenerator( |
| 244 | const DependencyOutputOptions &Opts) |
| 245 | : OutputFile(Opts.OutputFile), Targets(Opts.Targets), |
| 246 | IncludeSystemHeaders(Opts.IncludeSystemHeaders), |
| 247 | PhonyTarget(Opts.UsePhonyTargets), |
| 248 | AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false), |
| 249 | IncludeModuleFiles( |
| 250 | static_cast<ModuleFileDepsKind>(Opts.IncludeModuleFiles)), |
| 251 | OutputFormat(Opts.OutputFormat), InputFileIndex(0) { |
| 252 | for (const auto & : Opts.ExtraDeps) { |
| 253 | if (addDependency(Filename: ExtraDep.first)) |
| 254 | ++InputFileIndex; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) { |
| 259 | // Disable the "file not found" diagnostic if the -MG option was given. |
| 260 | if (AddMissingHeaderDeps) |
| 261 | PP.SetSuppressIncludeNotFoundError(true); |
| 262 | |
| 263 | DependencyCollector::attachToPreprocessor(PP); |
| 264 | } |
| 265 | |
| 266 | bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule, |
| 267 | bool IsSystem, bool IsModuleFile, |
| 268 | bool IsDirectModuleImport, |
| 269 | bool IsMissing) { |
| 270 | if (IsMissing) { |
| 271 | // Handle the case of missing file from an inclusion directive. |
| 272 | if (AddMissingHeaderDeps) |
| 273 | return true; |
| 274 | SeenMissingHeader = true; |
| 275 | return false; |
| 276 | } |
| 277 | if (IsModuleFile) { |
| 278 | if (IncludeModuleFiles == MFDK_None) |
| 279 | return false; |
| 280 | if (IncludeModuleFiles == MFDK_Direct && !IsDirectModuleImport) |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | if (isSpecialFilename(Filename)) |
| 285 | return false; |
| 286 | |
| 287 | if (IncludeSystemHeaders) |
| 288 | return true; |
| 289 | |
| 290 | return !IsSystem; |
| 291 | } |
| 292 | |
| 293 | void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) { |
| 294 | outputDependencyFile(Diags); |
| 295 | } |
| 296 | |
| 297 | void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) { |
| 298 | if (SeenMissingHeader) { |
| 299 | llvm::sys::fs::remove(path: OutputFile); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | std::error_code EC; |
| 304 | llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF); |
| 305 | if (EC) { |
| 306 | Diags.Report(DiagID: diag::err_fe_error_opening) << OutputFile << EC.message(); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | outputDependencyFile(OS); |
| 311 | } |
| 312 | |
| 313 | void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { |
| 314 | clang::printMakeDependencyFile(OS, Targets, Files: getDependencies(), Format: OutputFormat, |
| 315 | PhonyTargets: PhonyTarget, InputFileIndex); |
| 316 | } |
| 317 | |