23#include "llvm/ADT/StringSet.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/VirtualFileSystem.h"
27#include "llvm/Support/raw_ostream.h"
29#include <system_error>
34struct DepCollectorPPCallbacks :
public PPCallbacks {
38 : DepCollector(L), PP(PP) {}
43 if (Reason != PPCallbacks::LexedFileChangeReason::EnterFile)
49 if (std::optional<StringRef>
Filename =
52 llvm::sys::path::remove_leading_dotslash(*
Filename),
60 llvm::sys::path::remove_leading_dotslash(SkippedFile.
getName());
70 assert(
File &&
"expected to only be called when the file is found");
72 llvm::sys::path::remove_leading_dotslash(
File->getName());
84 StringRef RelativePath,
const Module *SuggestedModule,
100 llvm::sys::path::remove_leading_dotslash(
File->getName());
113 llvm::sys::path::remove_leading_dotslash(
File->getName());
130 bool IsSystem)
override {
143 : DepCollector(L), FileMgr(FileMgr) {}
155 bool IsOverridden,
bool IsExplicitModule)
override {
156 if (IsOverridden || IsExplicitModule)
173 bool FromModule,
bool IsSystem,
181 StringRef SearchPath;
185 llvm::sys::path::native(TmpPath);
186 std::transform(TmpPath.begin(), TmpPath.end(), TmpPath.begin(), ::tolower);
187 SearchPath = TmpPath.str();
192 if (Seen.insert(SearchPath).second) {
193 Dependencies.push_back(std::string(
Filename));
204 bool IsSystem,
bool IsModuleFile,
212 PP.
addPPCallbacks(std::make_unique<DepCollectorPPCallbacks>(*
this, PP));
214 std::make_unique<DepCollectorMMCallbacks>(*
this));
218 std::make_unique<DepCollectorASTListener>(*
this, R.
getFileManager()));
223 : OutputFile(Opts.OutputFile), Targets(Opts.Targets),
224 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
225 PhonyTarget(Opts.UsePhonyTargets),
226 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(
false),
227 IncludeModuleFiles(Opts.IncludeModuleFiles),
228 OutputFormat(Opts.OutputFormat), InputFileIndex(0) {
229 for (
const auto &ExtraDep : Opts.
ExtraDeps) {
237 if (AddMissingHeaderDeps)
245 bool IsSystem,
bool IsModuleFile,
249 if (AddMissingHeaderDeps)
251 SeenMissingHeader =
true;
254 if (IsModuleFile && !IncludeModuleFiles)
260 if (IncludeSystemHeaders)
323 llvm::sys::path::native(
Filename.str(), NativePath);
328 if (FS !=
nullptr && llvm::sys::path::is_absolute(NativePath)) {
330 std::error_code EC = FS->getRealPath(NativePathTmp, NativePath);
332 NativePath = NativePathTmp;
339 if (NativePath.find_first_of(
" #${}^!") != StringRef::npos)
340 OS <<
'\"' << NativePath <<
'\"';
346 for (
unsigned i = 0, e = NativePath.size(); i != e; ++i) {
347 if (NativePath[i] ==
'#')
349 else if (NativePath[i] ==
' ') {
352 while (j > 0 && NativePath[--j] ==
'\\')
354 }
else if (NativePath[i] ==
'$')
361 if (SeenMissingHeader) {
362 llvm::sys::fs::remove(OutputFile);
367 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF);
369 Diags.
Report(diag::err_fe_error_opening) << OutputFile << EC.message();
381 const unsigned MaxColumns = 75;
382 unsigned Columns = 0;
384 for (StringRef
Target : Targets) {
385 unsigned N =
Target.size();
388 }
else if (Columns + N + 2 > MaxColumns) {
405 for (StringRef
File : Files) {
406 if (
File ==
"<stdin>")
411 unsigned N =
File.size();
412 if (Columns + (N + 1) + 2 > MaxColumns) {
423 if (PhonyTarget && !Files.empty()) {
425 for (
auto I = Files.begin(),
E = Files.end(); I !=
E; ++I) {
426 if (Index++ == InputFileIndex)
static void printFilename(raw_ostream &OS, llvm::vfs::FileSystem *FS, StringRef Filename, DependencyOutputFormat OutputFormat)
Print the filename, with escaping or quoting that accommodates the three most likely tools that use d...
static bool isSpecialFilename(StringRef Filename)
Defines the clang::FileManager interface and associated types.
llvm::MachO::FileType FileType
llvm::MachO::Target Target
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
Abstract interface for callback invocations by the ASTReader.
virtual bool needsInputFileVisitation()
Returns true if this ASTReaderListener wants to receive the input files of the AST file via visitInpu...
virtual bool visitInputFile(StringRef Filename, bool isSystem, bool isOverridden, bool isExplicitModule)
if needsInputFileVisitation returns true, this is called for each non-system input file of the AST Fi...
virtual void visitModuleFile(StringRef Filename, serialization::ModuleKind Kind)
This is called for each AST file loaded.
virtual bool needsSystemInputFileVisitation()
Returns true if this ASTReaderListener wants to receive the system input files of the AST file via vi...
Reads an AST files chain containing the contents of a translation unit.
void addListener(std::unique_ptr< ASTReaderListener > L)
Add an AST callback listener.
FileManager & getFileManager() const
Represents a character-granular source range.
An interface for collecting the dependencies of a compilation.
virtual void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing)
Add a dependency Filename if it has not been seen before and sawDependency() returns true.
bool addDependency(StringRef Filename)
Return true if the filename was added to the list of dependencies, false otherwise.
virtual void attachToPreprocessor(Preprocessor &PP)
ArrayRef< std::string > getDependencies() const
virtual void finishedMainFile(DiagnosticsEngine &Diags)
Called when the end of the main file is reached.
virtual void attachToASTReader(ASTReader &R)
virtual ~DependencyCollector()
virtual bool needSystemDependencies()
Return true if system files should be passed to sawDependency().
virtual bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing)
Called when a new file is seen.
void outputDependencyFile(llvm::raw_ostream &OS)
void attachToPreprocessor(Preprocessor &PP) override
void finishedMainFile(DiagnosticsEngine &Diags) override
Called when the end of the main file is reached.
bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsMissing) final
Called when a new file is seen.
DependencyFileGenerator(const DependencyOutputOptions &Opts)
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
std::vector< std::pair< std::string, ExtraDepKind > > ExtraDeps
A list of extra dependencies (filename and kind) to be used for every target.
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
StringRef getName() const
The name of this FileEntry.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
Record the location of an inclusion directive, such as an #include or #import statement.
A mechanism to observe the actions of the module map parser as it reads module map files.
virtual void moduleMapFileRead(SourceLocation FileStart, FileEntryRef File, bool IsSystem)
Called when a module map file has been read.
void addModuleMapCallbacks(std::unique_ptr< ModuleMapCallbacks > Callback)
Add a module map callback.
Describes a module or submodule.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
virtual void HasEmbed(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File)
Hook called when a '__has_embed' directive is read.
virtual void EndOfMainFile()
Callback invoked when the end of the main file is reached.
virtual void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok, SrcMgr::CharacteristicKind FileType)
Callback invoked whenever a source file is skipped as the result of header guard optimization.
virtual void EmbedDirective(SourceLocation HashLoc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, const LexEmbedParametersResult &Params)
Callback invoked whenever an embed directive has been processed, regardless of whether the embed will...
virtual void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled, OptionalFileEntryRef File, SrcMgr::CharacteristicKind FileType)
Hook called when a '__has_include' or '__has_include_next' directive is read.
virtual void LexedFileChanged(FileID FID, LexedFileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID, SourceLocation Loc)
Callback invoked whenever the Lexer moves to a different file for lexing.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
SourceManager & getSourceManager() const
FileManager & getFileManager() const
HeaderSearch & getHeaderSearchInfo() const
void SetSuppressIncludeNotFoundError(bool Suppress)
DiagnosticsEngine & getDiagnostics() const
Encodes a location in the source.
std::optional< StringRef > getNonBuiltinFilenameForID(FileID FID) const
Returns the filename for the provided FileID, unless it's a built-in buffer that's not represented by...
Token - This structure provides full information about a lexed token.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
bool isSystem(CharacteristicKind CK)
Determine whether a file / directory characteristic is for system code.
ModuleKind
Specifies the kind of module that has been loaded.
The JSON file list parser is used to communicate input to InstallAPI.
DependencyOutputFormat
DependencyOutputFormat - Format for the compiler dependency file.