Path: blob/main/contrib/llvm-project/clang/lib/Frontend/DependencyFile.cpp
35232 views
//===--- DependencyFile.cpp - Generate dependency file --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This code generates dependency files.9//10//===----------------------------------------------------------------------===//1112#include "clang/Frontend/Utils.h"13#include "clang/Basic/FileManager.h"14#include "clang/Basic/SourceManager.h"15#include "clang/Frontend/DependencyOutputOptions.h"16#include "clang/Frontend/FrontendDiagnostic.h"17#include "clang/Lex/DirectoryLookup.h"18#include "clang/Lex/ModuleMap.h"19#include "clang/Lex/PPCallbacks.h"20#include "clang/Lex/Preprocessor.h"21#include "clang/Serialization/ASTReader.h"22#include "llvm/ADT/StringSet.h"23#include "llvm/Support/FileSystem.h"24#include "llvm/Support/Path.h"25#include "llvm/Support/raw_ostream.h"26#include <optional>2728using namespace clang;2930namespace {31struct DepCollectorPPCallbacks : public PPCallbacks {32DependencyCollector &DepCollector;33Preprocessor &PP;34DepCollectorPPCallbacks(DependencyCollector &L, Preprocessor &PP)35: DepCollector(L), PP(PP) {}3637void LexedFileChanged(FileID FID, LexedFileChangeReason Reason,38SrcMgr::CharacteristicKind FileType, FileID PrevFID,39SourceLocation Loc) override {40if (Reason != PPCallbacks::LexedFileChangeReason::EnterFile)41return;4243// Dependency generation really does want to go all the way to the44// file entry for a source location to find out what is depended on.45// We do not want #line markers to affect dependency generation!46if (std::optional<StringRef> Filename =47PP.getSourceManager().getNonBuiltinFilenameForID(FID))48DepCollector.maybeAddDependency(49llvm::sys::path::remove_leading_dotslash(*Filename),50/*FromModule*/ false, isSystem(FileType), /*IsModuleFile*/ false,51/*IsMissing*/ false);52}5354void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,55SrcMgr::CharacteristicKind FileType) override {56StringRef Filename =57llvm::sys::path::remove_leading_dotslash(SkippedFile.getName());58DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,59/*IsSystem=*/isSystem(FileType),60/*IsModuleFile=*/false,61/*IsMissing=*/false);62}6364void EmbedDirective(SourceLocation, StringRef, bool,65OptionalFileEntryRef File,66const LexEmbedParametersResult &) override {67assert(File && "expected to only be called when the file is found");68StringRef FileName =69llvm::sys::path::remove_leading_dotslash(File->getName());70DepCollector.maybeAddDependency(FileName,71/*FromModule*/ false,72/*IsSystem*/ false,73/*IsModuleFile*/ false,74/*IsMissing*/ false);75}7677void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,78StringRef FileName, bool IsAngled,79CharSourceRange FilenameRange,80OptionalFileEntryRef File, StringRef SearchPath,81StringRef RelativePath, const Module *SuggestedModule,82bool ModuleImported,83SrcMgr::CharacteristicKind FileType) override {84if (!File)85DepCollector.maybeAddDependency(FileName, /*FromModule*/ false,86/*IsSystem*/ false,87/*IsModuleFile*/ false,88/*IsMissing*/ true);89// Files that actually exist are handled by FileChanged.90}9192void HasEmbed(SourceLocation, StringRef, bool,93OptionalFileEntryRef File) override {94if (!File)95return;96StringRef Filename =97llvm::sys::path::remove_leading_dotslash(File->getName());98DepCollector.maybeAddDependency(Filename,99/*FromModule=*/false, false,100/*IsModuleFile=*/false,101/*IsMissing=*/false);102}103104void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,105OptionalFileEntryRef File,106SrcMgr::CharacteristicKind FileType) override {107if (!File)108return;109StringRef Filename =110llvm::sys::path::remove_leading_dotslash(File->getName());111DepCollector.maybeAddDependency(Filename, /*FromModule=*/false,112/*IsSystem=*/isSystem(FileType),113/*IsModuleFile=*/false,114/*IsMissing=*/false);115}116117void EndOfMainFile() override {118DepCollector.finishedMainFile(PP.getDiagnostics());119}120};121122struct DepCollectorMMCallbacks : public ModuleMapCallbacks {123DependencyCollector &DepCollector;124DepCollectorMMCallbacks(DependencyCollector &DC) : DepCollector(DC) {}125126void moduleMapFileRead(SourceLocation Loc, FileEntryRef Entry,127bool IsSystem) override {128StringRef Filename = Entry.getName();129DepCollector.maybeAddDependency(Filename, /*FromModule*/ false,130/*IsSystem*/ IsSystem,131/*IsModuleFile*/ false,132/*IsMissing*/ false);133}134};135136struct DepCollectorASTListener : public ASTReaderListener {137DependencyCollector &DepCollector;138FileManager &FileMgr;139DepCollectorASTListener(DependencyCollector &L, FileManager &FileMgr)140: DepCollector(L), FileMgr(FileMgr) {}141bool needsInputFileVisitation() override { return true; }142bool needsSystemInputFileVisitation() override {143return DepCollector.needSystemDependencies();144}145void visitModuleFile(StringRef Filename,146serialization::ModuleKind Kind) override {147DepCollector.maybeAddDependency(Filename, /*FromModule*/ true,148/*IsSystem*/ false, /*IsModuleFile*/ true,149/*IsMissing*/ false);150}151bool visitInputFile(StringRef Filename, bool IsSystem,152bool IsOverridden, bool IsExplicitModule) override {153if (IsOverridden || IsExplicitModule)154return true;155156// Run this through the FileManager in order to respect 'use-external-name'157// in case we have a VFS overlay.158if (auto FE = FileMgr.getOptionalFileRef(Filename))159Filename = FE->getName();160161DepCollector.maybeAddDependency(Filename, /*FromModule*/ true, IsSystem,162/*IsModuleFile*/ false,163/*IsMissing*/ false);164return true;165}166};167} // end anonymous namespace168169void DependencyCollector::maybeAddDependency(StringRef Filename,170bool FromModule, bool IsSystem,171bool IsModuleFile,172bool IsMissing) {173if (sawDependency(Filename, FromModule, IsSystem, IsModuleFile, IsMissing))174addDependency(Filename);175}176177bool DependencyCollector::addDependency(StringRef Filename) {178StringRef SearchPath;179#ifdef _WIN32180// Make the search insensitive to case and separators.181llvm::SmallString<256> TmpPath = Filename;182llvm::sys::path::native(TmpPath);183std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower);184SearchPath = TmpPath.str();185#else186SearchPath = Filename;187#endif188189if (Seen.insert(SearchPath).second) {190Dependencies.push_back(std::string(Filename));191return true;192}193return false;194}195196static bool isSpecialFilename(StringRef Filename) {197return Filename == "<built-in>";198}199200bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,201bool IsSystem, bool IsModuleFile,202bool IsMissing) {203return !isSpecialFilename(Filename) &&204(needSystemDependencies() || !IsSystem);205}206207DependencyCollector::~DependencyCollector() { }208void DependencyCollector::attachToPreprocessor(Preprocessor &PP) {209PP.addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*this, PP));210PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(211std::make_unique<DepCollectorMMCallbacks>(*this));212}213void DependencyCollector::attachToASTReader(ASTReader &R) {214R.addListener(215std::make_unique<DepCollectorASTListener>(*this, R.getFileManager()));216}217218DependencyFileGenerator::DependencyFileGenerator(219const DependencyOutputOptions &Opts)220: OutputFile(Opts.OutputFile), Targets(Opts.Targets),221IncludeSystemHeaders(Opts.IncludeSystemHeaders),222PhonyTarget(Opts.UsePhonyTargets),223AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false),224IncludeModuleFiles(Opts.IncludeModuleFiles),225OutputFormat(Opts.OutputFormat), InputFileIndex(0) {226for (const auto &ExtraDep : Opts.ExtraDeps) {227if (addDependency(ExtraDep.first))228++InputFileIndex;229}230}231232void DependencyFileGenerator::attachToPreprocessor(Preprocessor &PP) {233// Disable the "file not found" diagnostic if the -MG option was given.234if (AddMissingHeaderDeps)235PP.SetSuppressIncludeNotFoundError(true);236237DependencyCollector::attachToPreprocessor(PP);238}239240bool DependencyFileGenerator::sawDependency(StringRef Filename, bool FromModule,241bool IsSystem, bool IsModuleFile,242bool IsMissing) {243if (IsMissing) {244// Handle the case of missing file from an inclusion directive.245if (AddMissingHeaderDeps)246return true;247SeenMissingHeader = true;248return false;249}250if (IsModuleFile && !IncludeModuleFiles)251return false;252253if (isSpecialFilename(Filename))254return false;255256if (IncludeSystemHeaders)257return true;258259return !IsSystem;260}261262void DependencyFileGenerator::finishedMainFile(DiagnosticsEngine &Diags) {263outputDependencyFile(Diags);264}265266/// Print the filename, with escaping or quoting that accommodates the three267/// most likely tools that use dependency files: GNU Make, BSD Make, and268/// NMake/Jom.269///270/// BSD Make is the simplest case: It does no escaping at all. This means271/// characters that are normally delimiters, i.e. space and # (the comment272/// character) simply aren't supported in filenames.273///274/// GNU Make does allow space and # in filenames, but to avoid being treated275/// as a delimiter or comment, these must be escaped with a backslash. Because276/// backslash is itself the escape character, if a backslash appears in a277/// filename, it should be escaped as well. (As a special case, $ is escaped278/// as $$, which is the normal Make way to handle the $ character.)279/// For compatibility with BSD Make and historical practice, if GNU Make280/// un-escapes characters in a filename but doesn't find a match, it will281/// retry with the unmodified original string.282///283/// GCC tries to accommodate both Make formats by escaping any space or #284/// characters in the original filename, but not escaping backslashes. The285/// apparent intent is so that filenames with backslashes will be handled286/// correctly by BSD Make, and by GNU Make in its fallback mode of using the287/// unmodified original string; filenames with # or space characters aren't288/// supported by BSD Make at all, but will be handled correctly by GNU Make289/// due to the escaping.290///291/// A corner case that GCC gets only partly right is when the original filename292/// has a backslash immediately followed by space or #. GNU Make would expect293/// this backslash to be escaped; however GCC escapes the original backslash294/// only when followed by space, not #. It will therefore take a dependency295/// from a directive such as296/// #include "a\ b\#c.h"297/// and emit it as298/// a\\\ b\\#c.h299/// which GNU Make will interpret as300/// a\ b\301/// followed by a comment. Failing to find this file, it will fall back to the302/// original string, which probably doesn't exist either; in any case it won't303/// find304/// a\ b\#c.h305/// which is the actual filename specified by the include directive.306///307/// Clang does what GCC does, rather than what GNU Make expects.308///309/// NMake/Jom has a different set of scary characters, but wraps filespecs in310/// double-quotes to avoid misinterpreting them; see311/// https://msdn.microsoft.com/en-us/library/dd9y37ha.aspx for NMake info,312/// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx313/// for Windows file-naming info.314static void PrintFilename(raw_ostream &OS, StringRef Filename,315DependencyOutputFormat OutputFormat) {316// Convert filename to platform native path317llvm::SmallString<256> NativePath;318llvm::sys::path::native(Filename.str(), NativePath);319320if (OutputFormat == DependencyOutputFormat::NMake) {321// Add quotes if needed. These are the characters listed as "special" to322// NMake, that are legal in a Windows filespec, and that could cause323// misinterpretation of the dependency string.324if (NativePath.find_first_of(" #${}^!") != StringRef::npos)325OS << '\"' << NativePath << '\"';326else327OS << NativePath;328return;329}330assert(OutputFormat == DependencyOutputFormat::Make);331for (unsigned i = 0, e = NativePath.size(); i != e; ++i) {332if (NativePath[i] == '#') // Handle '#' the broken gcc way.333OS << '\\';334else if (NativePath[i] == ' ') { // Handle space correctly.335OS << '\\';336unsigned j = i;337while (j > 0 && NativePath[--j] == '\\')338OS << '\\';339} else if (NativePath[i] == '$') // $ is escaped by $$.340OS << '$';341OS << NativePath[i];342}343}344345void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) {346if (SeenMissingHeader) {347llvm::sys::fs::remove(OutputFile);348return;349}350351std::error_code EC;352llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);353if (EC) {354Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message();355return;356}357358outputDependencyFile(OS);359}360361void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {362// Write out the dependency targets, trying to avoid overly long363// lines when possible. We try our best to emit exactly the same364// dependency file as GCC>=10, assuming the included files are the365// same.366const unsigned MaxColumns = 75;367unsigned Columns = 0;368369for (StringRef Target : Targets) {370unsigned N = Target.size();371if (Columns == 0) {372Columns += N;373} else if (Columns + N + 2 > MaxColumns) {374Columns = N + 2;375OS << " \\\n ";376} else {377Columns += N + 1;378OS << ' ';379}380// Targets already quoted as needed.381OS << Target;382}383384OS << ':';385Columns += 1;386387// Now add each dependency in the order it was seen, but avoiding388// duplicates.389ArrayRef<std::string> Files = getDependencies();390for (StringRef File : Files) {391if (File == "<stdin>")392continue;393// Start a new line if this would exceed the column limit. Make394// sure to leave space for a trailing " \" in case we need to395// break the line on the next iteration.396unsigned N = File.size();397if (Columns + (N + 1) + 2 > MaxColumns) {398OS << " \\\n ";399Columns = 2;400}401OS << ' ';402PrintFilename(OS, File, OutputFormat);403Columns += N + 1;404}405OS << '\n';406407// Create phony targets if requested.408if (PhonyTarget && !Files.empty()) {409unsigned Index = 0;410for (auto I = Files.begin(), E = Files.end(); I != E; ++I) {411if (Index++ == InputFileIndex)412continue;413PrintFilename(OS, *I, OutputFormat);414OS << ":\n";415}416}417}418419420